non-static variable number cannot be referenced from a static context
Do you see this message looks familiar?If you are unsure about the static and non-static, read on and I will clear your doubts in few minutes. In addition, I’ll share something interesting which you may not know before.Generally, static is a modifier applying for member variables and methods.public class A { public int x; }You know, we can create as many instances of the class A as we want. For example:
A a1 = new A(); a1.x = 1; A a2 = new A(); a2.x = 2; System.out.println("a1.x = " + a1.x); System.out.println("a2.x = " + a2.x);Here, x is a non-static variable, and each object of the class A has its own copy of x, thus the following output:
a1.x = 1 a2.x = 2To remember, a non-static variable belongs to an instance (object) of a class. Each object has its own copy of the variable.
public class A { public static int x; }Note that the static modifier is used before the variable type. And here’s the same test code as above:
A a1 = new A(); a1.x = 1; A a2 = new A(); a2.x = 2; System.out.println("a1.x = " + a1.x); System.out.println("a2.x = " + a2.x);But the output is different:
a1.x = 2 a2.x = 2Oh, why do we get the same value of x? It’s because a static variable is shared among all instances of a class. In other words, a static variable is unique: there’s only one copy which is shared by all objects.Hence there’s no need of creating an object to access the static variable. We can write:
A.x = 3; System.out.println("A.x = " + A.x);Output:
A.x = 3NOTE: We use the class name to access the static members without creating an object.
public class Dog { public void bark() { System.out.println("Go Go!"); } }Similar to non-static variable, we can only invoke a non-static method via an instance of the class. For example:
Dog dog = new Dog(); dog.bark();
public class Math { public static int add(int a, int b) { return a + b; } }A static method is shared by all instances of a class, so there’s no need of creating a specific object to invoke the static method. For example:
int sum = Math.add(7, 8); System.out.println("sum = " + sum);Output:
sum = 15
public class Test { public int number; public static void main(String[] args) { number = 5; } }Here, main() is a static method and number is a non-static variable. The number variable must belong to an instance, whereas the main() method does not, hence the Java compiler complain:
non-static variable number cannot be referenced from a static context
That’s simple! Got it?On the contrary, we can always access static members from anywhere. For example:public class B { static int z; public void bar() { z = 1000; // Okay, fine! } }So to remember:
- Static members can be accessed from anywhere.
- Non-static members cannot be accessed from static context like in a method.
* Is there static class?No, there is no static class. But there’s inner static class. * Static members can be inherited?Yes, of course. A subclass inherits all accessible static members from its super class. * Static methods can be overridden?No. If we have a method with the same signature in the subclass, then that method hides the one in the super class, not overridden. Here’s an example:
public class Parent { protected void foo() { System.out.println("Parent->foo"); } } public class Child extends Parent { public static void foo() { System.out.println("Child->foo"); } }Here’s the static foo() method in the Child class hides the one in the Parent class.