Differences between static and non-static stuffs in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 16 August 2019   |   Print Email
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.1. What is a non-static variable?
A non-static variable is a regular one. It’s better to understand the concept by looking at some examples. Given the following class: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.
2. What is a static variable?
A static variable is modified by the static modifier. Let’s modify the class A above by making the x variable static: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.
3. Non-static method
A non-static method is just a regular one. For example: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();
4. Static method
Similar to static variable, a static method is modified by the static modifier. Here’s an example: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
5. Why do we get the error: non-static variable number cannot be referenced from a static context?
How many times did you get this error? I bet you faced it several times in your life, right?Until now I think you know the reason. Let me explain.When we are trying to access a non-static variable or a non-static method from a static method, we’ll get such error.Let’s see the following example: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.
6. When do we use static and non-static members?
Generally, non-static members are preferred. As in OOP we create objects with states and behaviors. And different objects have different states, so we should use non-static members if they are specific to each object.Use static variables if they should be shared among all objects, e.g. a variable to keep track the number of instances, a constant, etc.Use static methods if they do not depend on states of any objects, e.g. utility methods that do calculation.Related Tutorials:
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- Java Access Modifiers Examples: public, protected, private and default
- 10 Java Core Best Practices Every Java Programmer Should Know
- Java Variables Passing Examples - Pass-by-value or Pass-by-reference?
Comments