Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

like image 694
Rohit Sharma Avatar asked Nov 19 '10 15:11

Rohit Sharma


People also ask

How do you fix non static variable this Cannot be referenced from a static context?

Therefore, this issue can be solved by addressing the variables with the object names. In short, we always need to create an object in order to refer to a non-static variable from a static context. Whenever a new instance is created, a new copy of all the non-static variables and methods are created.

Can you access a non static variable in the static context?

And if no class instance is created, the non-static variable is never initialized and there is no value to reference. For the same reasons, a non-static method cannot be referenced from a static context, either, as the compiler cannot tell which particular object the non-static member belongs to.

How do you assign a non static variable to a static variable?

You cannot assign the result of a non-static method to a static variable. Instead, you would need to convert the getIPZip method to be a static method of your MyProps class, then you could assign its result to yor IPZip variable like this. public static String IPZip = MyProps. getIPZip("IPZip");

Why non static variables are not allowed in static Block?

Non-static variables are part of the objects themselves. To use a non-static variable, you need to specify which instance of the class the variable belongs to. ... In other words, non-static data cannot be used in static methods because there is no well-defined variable to operate on.


1 Answers

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.

like image 138
Chris Hutchinson Avatar answered Sep 29 '22 12:09

Chris Hutchinson