开发者

non static variable name cannot be referenced from a static context [duplicate]

This question already has answers here: Non-static variable cannot be referenced from开发者_JAVA百科 a static context (15 answers) Closed 5 years ago.
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


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:

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:

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.


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.


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.

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);
}


One option is what Chris Hutchinson mentioned. The other one is to declare them static.

main is a static method. So name and album have to be declared as static.

private static String name;
private static String album;


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜