Java 不能从静态上下文中引用非静态变量名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4226734/
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
non static variable name cannot be referenced from a static context
提问by Rohit Sharma
class Singer
{
String name;
String album;
public Singer(){
name="Whitney Houson";
album="Latest Releases";
}
public static void main(String[] args)
{
System.out.println("Name of the singer is "+name);
System.out.println("Album Information stored for "+album);
}
}
When i run this code i am finding error which says that non static variable name cannot be referenced from a static context
当我运行此代码时,我发现错误表明无法从静态上下文中引用非静态变量名
回答by Chris Hutchinson
That's because the variables name and album do not exist in the main procedure, because it's static, which means it cannot access instance-level members. You will need an instance of the Singer class, like this:
那是因为变量name 和album 在main 过程中不存在,因为它是静态的,这意味着它不能访问实例级成员。您将需要 Singer 类的一个实例,如下所示:
public static void main(String[] args) {
Singer s = new Singer();
System.out.println("Name of the singer is " + s.name);
System.out.println("Album information stored for " + s.album);
}
However, unless you declare your name/album members with a public access modifier, the above code will fail to compile. I recommended writing a getter for each member (getName(), getAlbum(), etc), in order to benefit from encapsulation. Like this:
但是,除非您使用公共访问修饰符声明您的姓名/专辑成员,否则上述代码将无法编译。我建议为每个成员(getName()、getAlbum() 等)编写一个 getter,以便从封装中受益。像这样:
class Singer {
private String name;
private String album;
public Singer() {
this.name = "Whitney Houston";
this.album = "Latest Releases";
}
public String getName() {
return this.name;
}
public String getAlbum() {
return this.album;
}
public static void main(String[] args) {
Singer s = new Singer();
System.out.println("Name of the singer is " + s.getName());
System.out.println("Album information stored for " + s.getAlbum());
}
}
Another alternative would be to declare name and album as static, then you can reference them in the way you originally intended.
另一种选择是将名称和专辑声明为静态,然后您可以按照您最初的预期方式引用它们。
回答by CoolBeans
One option is what Chris Hutchinson mentioned. The other one is to declare them static.
一种选择是 Chris Hutchinson 提到的。另一种是将它们声明为静态。
main is a static method. So name and album have to be declared as static.
main 是一个静态方法。所以 name 和专辑必须声明为静态的。
private static String name;
private static String album;
回答by Jonathon Faust
Main is a static method. Instance variables (variables defined in the class but not marked as static) cannot be accessed from a static method without referencing an instance of the class.
Main 是一个静态方法。实例变量(在类中定义但未标记为静态的变量)不能在不引用类的实例的情况下从静态方法访问。
public static void main(String[] args)
{
Singer singer = new Singer();
System.out.println("Name of the singer is " + singer.name);
System.out.println("Album Information stored for " + singer.album);
}
回答by MattC
To expand more on Chris' answer, you can technically have as many instances of the Singer class as your memory can support, but there is only ever one instance of the main function running. This means that attempting to access those variables from the static function means it has no idea which instance of the variable it should be accessing, hence the error.
为了进一步扩展 Chris 的答案,从技术上讲,您可以拥有尽可能多的 Singer 类实例,只要您的内存可以支持,但只有一个 main 函数实例正在运行。这意味着尝试从静态函数访问这些变量意味着它不知道应该访问变量的哪个实例,因此会出现错误。
You can make the variables local to the main function, but that would probably defeat the purpose of the program then since logic would dictate there can be more than one singer (most likely). A better plan of attack would be to create a generic class that houses the main function, and then create a Singer class within that (or elsewhere) and instantiate X instances of that class in your main function and go from there.
您可以将变量设置为 main 函数的局部变量,但这可能会破坏程序的目的,因为逻辑会规定可能有多个歌手(最有可能)。更好的攻击计划是创建一个包含 main 函数的通用类,然后在该类(或其他地方)中创建一个 Singer 类,并在您的 main 函数中实例化该类的 X 个实例,然后从那里开始。
回答by Will Marcouiller
A non-static member or class needs to be instanced in order to exist. Then, accessing a non-static member or object from a static member does not guarantee that this member or object is instantiated, then access to it is impossible.
非静态成员或类需要实例化才能存在。那么,从静态成员访问非静态成员或对象并不能保证该成员或对象被实例化,那么访问它是不可能的。
You will need to create an instance of your non-static object within your static context to make it.
您需要在静态上下文中创建一个非静态对象的实例来制作它。
class Singer {
String name;
String album;
// You will need the following to make your code compile,
// and the call to these getters within your 'main' function.
public getName() {
return name;
}
public getAlbum() {
return album;
}
public Singer() {
name="Whitney Houson";
album="Latest Releases";
}
}
public static void main(String[] args) {
Singer singer = new Singer();
System.out.println("Name of the singer is " + singer.getName());
System.out.println("Album Information stored for " + singer.getAlbum());
}
This way, you include the instantiation of the Singer
object into a static object, thuis assuring it is instantiated properly before it is accessed.
通过这种方式,您可以将Singer
对象的实例化包含在静态对象中,从而确保在访问之前正确实例化它。