-2
public class HelloWorld {
    String name = "asad";

    public static void main(String []args){
        System.out.println("hello world" + name);//Display the string

        }
}

Error: Cannot make a static reference to the non-static field name . This is a code i tried, copy pasted yet it isn't working

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 1
    You would have to make `name` a static variable: `static String name = "asad";`. – AntonH Oct 09 '14 at 17:39
  • 1
    This is a very trivial error. The short answer is to make `name` static. I suggest reading up on static variables. – Vivin Paliath Oct 09 '14 at 17:39
  • You need to understand static vs non-static first - http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Dan W Oct 09 '14 at 17:39
  • if you just want it to not give you an error, you can declare `name` like: `static String name;` – Sam I am says Reinstate Monica Oct 09 '14 at 17:39
  • 2
    possible duplicate of [Cannot Make Static Reference to Non-Static Method](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) – StackFlowed Oct 09 '14 at 17:39
  • asad is a very bad man – Dima Oct 09 '14 at 17:41
  • 1
    @wrongAnswer - I strongly recommend changing your *username* :P. People would think twice before up-voting your comment / answer :P – TheLostMind Oct 09 '14 at 17:46
  • @TheLostMind I can't change it until 22 october till then I have to live with it :p... – StackFlowed Oct 09 '14 at 17:48
  • possible duplicate of [Getting error while using .nextInt()](http://stackoverflow.com/questions/19947636/getting-error-while-using-nextint) (a question with "cannot make static reference to nonstatic field") – Dennis Meng Oct 10 '14 at 06:49

8 Answers8

3

You have 2 options.

  1. Create an instance of HelloWorld and use new HelloWorld().name
  2. Make name static and use HelloWorld.name
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Make the string you're referring to static:

public class HelloWorld {
    static String name = "asad";

    public static void main(String []args){
        System.out.println("hello world" + name);//Display the string

    }
}

More on the difference between static and non-static fields/methods...

Mshnik
  • 7,032
  • 1
  • 25
  • 38
1

you can either make name static, or put it inside the main method:

public class HelloWorld {
public static void main(String []args){
    String name = "asad";
    System.out.println("hello world" + name);//Display the string

    }
}
luisluix
  • 547
  • 5
  • 17
1
public class HelloWorld {
    static String name = "asad";

public static void main(String []args){
    System.out.println("hello world" + name);//Display the string

    }
}

You can call only static attributes in a static method.

Mekap
  • 2,065
  • 14
  • 26
0

HelloWorld is an class. You can make multiple instances of this class.

Here's a sample making 2 instances of the class

HelloWorld helloWorld1 = new HelloWorld();
HelloWorld helloWorld2 = new HelloWorld();

right now, name is declared as an instance field. That means that each instance of HelloWorld will have 1.


now, the main method is a static method. That means that there is only 1 main method period. you do not get a different main method for each instance of HelloWorld. (main also happens to be a special static method that serves as the entry point of your program)

When you try to call name from main, your program doesn't have any instance of name to refer to. If you had declared multiple HelloWorld objects, it wouldn't know which one's name to use.


If you just want to call name from main, you can make name a static field. Remember, static means that you only have 1 name per program.

static String name = "asad";
0

You have three options:

  1. Create an instance of HelloWorld and get the field from the instance, new HelloWorld().name
  2. Change name to a static or class field
  3. Move the declaration of name into main()

So,

String name = "asad";
public static void main(String []args){
  System.out.println("hello world" + new HelloWorld().name);
}

Or,

static String name = "asad";
public static void main(String []args){
  System.out.println("hello world" + name);
}

Or,

public static void main(String []args){
  String name = "asad";
  System.out.println("hello world" + name);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Short answer: make name static; i.e:

static String name = "asad";

Long answer: You need to understand the difference between static and non-static. static properties and methods are attached to the class. This means that they are available without an instance of the class, and more importantly, shared between all instances (because they belong to the class itself).

On the other hand, non-static properties and methods belong to the instance of the class only. These are things that belong to a particular instance of the class.

The problem you are facing is that main is static. Since main is invoked on the class itself, it has no idea what name is, because name is attached to an instance. Hence name is undefined in the static context.

Another way, other than making name static, is to forcibly create an instance of the class and then reference name that way:

new HelloWorld().name;

But this isn't really good style. You need to think long and hard about what properties/methods need to be static, and which don't. Static properties introduce another wrinkle in the sense that you now have shared state, which can lead to interesting or undesirable behavior if you are not careful.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

Your problem is that main is static, meaning it exists without you having to initialize the class it's in. However, the string you are trying to print is not static, meaning it only exists if the class is initialized. The compiler sees that it's possible for main to be called without the object being created (because it's static), and that means it won't be able to access the string to print, because it doesn't exist, so it complains. You need to make the string you are trying to print static as well.

static String name = "asad";
Andrew Gies
  • 719
  • 8
  • 20