简体   繁体   English

我做错了什么? 静态错误[Java]

[英]What have I done wrong? Static Errors [Java]

It refrences a error that I cannot use static when I used my function generatecode() I wanted to see if I did my split correctly. 当我使用我的函数generatecode()时,它引用了一个我无法使用静态的错误。我想看看我是否正确地进行了拆分。 I am new and still need a bit of help. 我是新人,仍需要一些帮助。 I've seen something on creating a new class via in this case: TestFile variable = new TestFile(); 在这种情况下,我已经看到了创建新类的一些事情:TestFile variable = new TestFile(); I had no idea what this means. 我不知道这意味着什么。 Thanks! 谢谢!

    public class TestFile {

String[] preps = {
    "about", "above", "across", "after", "against",
    "along", "among", "around", "at", "before",
    "behind", "below", "beneath", "beside", "between",
    "by", "concerning", "down", "during", "except",
    "for", "from", "in", "inside", "into",
    "like", "near", "of", "onto", "out",
    "over", "through", "to", "toward", "under",
    "up", "upon", "with", "within", "without"
};

String[] errorpreps = {
    "will", "would", "shall", "should", "can",
    "could", "may", "might", "must", 
};

String[] question = {
};

public static void main(String[] args) {

    generatecode("hi");

};

public generatecode(String code){

    String prep = "";

    for (int i=0; i<preps.length; i++){

        prep = prep + preps[i];

    }

    System.out.println(prep);

    return prep;

}

public String printcode(String code){


    return "";

}

    }

Your method has the wrong access modifier: 您的方法具有错误的访问修饰符:

public generatecode(String code){

Should be 应该

public static String generatecode(String code){

Just to note 请注意

You didn't have a return type for the method either, so this really shouldn't compile. 你也没有这个方法的返回类型,所以这真的不应该编译。

Why is it like this? 为什么会这样?

Well, static methods like main(String[] args) are capable of being ran, when there is no object instance. 好吧,当没有对象实例时,可以运行像main(String[] args)这样的静态方法。 So you can call: 所以你可以打电话:

ClassName.method();

When you attempt to call an instance method from a static method, this means that you're trying to use code functionality that requires an object instance to exist. 当您尝试从静态方法调用实例方法时,这意味着您尝试使用需要对象实例存在的代码功能。 So just to recap: 所以回顾一下:

ClassName c = new ClassName();
c.instanceMethod(); // This is an instance method.

ClassName.staticMethod(); // This is a static method.

In your static main method, you don't have any instances of your TestFile class yet. static main方法中,您还没有TestFile类的任何实例。 To reference anything that is non- static , you need an instance of the class. 要引用非static任何内容,您需要该类的实例。 That is exactly what the line TestFile variable = new TestFile(); 这正是TestFile variable = new TestFile();TestFile variable = new TestFile(); does -- it creates a new instance of TestFile . 确实 - 它创建了一个新的TestFile实例。

Then you can call your method on your instance: 然后,您可以在您的实例上调用您的方法:

variable.generatecode("hi");

As @ChrisCooney has already pointed out, you don't have a return type for that method. 正如@ChrisCooney已经指出的那样,你没有该方法的返回类型。 All methods need a return type. 所有方法都需要返回类型。 In this case, you need to declare that your method returns a String , because that's what the method does return. 在这种情况下,您需要声明您的方法返回一个String ,因为这是该方法返回的内容。

generate is an instance method, and you're trying to invoke it from main , which is a static method. generate是一个实例方法,你试图从main调用它,这是一个静态方法。 To workaround it, you could do.- 要解决它,你可以这样做.-

(new TestFile()).generatecode("hi");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 Android 代码中做错了什么? - What have I done wrong in the Android code? 使用Eclipse进行Java获取无法解决扫描程序。 有人知道我做错了吗? - Using Eclipse for java getting cannot be resolved for scanner. Anyone know what I have done wrong? Java-来自切换案例的连续循环。 我在这里做错了什么? - Java - Continous loop from the switch cases. What have I done wrong here? 我的PeriodFormatter表现不像我期望的那样 - 我做错了什么? - My PeriodFormatter is not behaving as I expect - what have I done wrong? 我在这里做错了什么,我该如何纠正? - What have I done wrong here and how do I correct it? 有人能告诉我我做错了什么吗 - Can some one tell me what i have done wrong 为了使我的应用在启动时运行,我做错了什么? - What have I done wrong to make my app run at startup? 有人能告诉我我对 IntelliJ 做错了什么吗 - Can somebody tell me what I have done wrong to IntelliJ 我在几个方法上遇到多个“错误:缺少返回语句”错误。 我做错了什么,该如何解决? - I am encountering multiple “error: missing return statement” errors on a couple of my methods. What have I done wrong and how can I fix it? 使用 Java 中的 setter 方法访问通过静态方法创建的实例的私有字段出错了 - 可以做什么? - Accessing private fields of an instance created through a static method with a setter method in Java gone wrong - what can be done?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM