[英]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.