<identifier> java 编译中的预期错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44322212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<identifier> expected Error in java Compilation
提问by Akash Singh Sengar
As i am new in java. I have searched about static mean in java and i got solution on stack overflow herebut when i compiled it it is showing error. Can anybody suggest Where i am mistaking ?
因为我是 Java 新手。我在 java 中搜索了静态均值,我在这里得到了堆栈溢出的解决方案,但是当我编译它时,它显示错误。有人可以建议我错在哪里吗?
public class Hello
{
// value / method
public static String staticValue;
public String nonStaticValue;
}
class A
{
Hello hello = new Hello();
hello.staticValue = "abc";
hello.nonStaticValue = "xyz";
}
class B
{
Hello hello2 = new Hello(); // here staticValue = "abc"
hello2.staticValue; // will have value of "abc"
hello2.nonStaticValue; // will have value of null
}
回答by Mohammad Mudassir
well in class level you can only define attributes of that class, cant do any processing which you are doing in classA and classB. Processing can only be done in method.
在类级别,您只能定义该类的属性,不能执行您在 classA 和 classB 中所做的任何处理。处理只能在方法中完成。
Just add main method make objects there
只需在那里添加 main 方法 make objects
public class Hello
{
// value / method
public static String staticValue;
public String nonStaticValue;
public void main(String[] args){
Hello hello = new Hello();
Hello.staticValue = "abc";
hello.nonStaticValue = "xyz";
Hello hello2 = new Hello(); // here staticValue = "abc"
Hello.staticValue; // will have value of "abc"
hello2.nonStaticValue; // will have value of null
}
}
Main method is entry point of any program in java. Dont worry if you are confused where this main method is called.
Main方法是java中任何程序的入口点。如果您对调用此主要方法的位置感到困惑,请不要担心。
回答by sjana
First of all, to run Java files you need a public class which contains the main method. Changing variable content can only be done in a method.
首先,要运行 Java 文件,您需要一个包含 main 方法的公共类。更改变量内容只能在方法中完成。
public class Hello(){
public static String staticValue;
public String nonStaticValue;
public static void main(String[] args){
Hello hello = new Hello();
Hello.staticValue = "abc";
hello.nonStaticValue = "xyz";
Hello hello2 = new Hello();
System.out.println(hello2.staticValue);
System.out.println(hello2.nonStaticValue);
}
}