Static vs non-static variables in Java
This post will discuss the difference between static and non-static variables in Java.
In the previous post, we have discussed the difference between static and non-static methods in Java. This post provides an overview of the differences between static and non-static variables in Java.
- Like static methods, a static variable belongs to the class itself, and a non-static variable belongs to each instance of a class. Therefore, the value of a static variable remains the same for each instance of the class, but the same cannot be said for the non-static variable.
- We cannot access non-static variables inside a static method without an instance of its class. A static method can only access static variables while a non-static method can access both static and non-static variables.
- Static variables reduce the memory footprint of the program. This is because the memory is allocated only once for a static variable during the time of class loading, while for a non-static variable, memory is allocated every time an instance of the class is created.
- Static variables are usually declared as final in Java. This ensures the value never gets changed after its initialization. This is very useful when we need a single copy of the variable to be shared across all class instances.
- Static variables can be accessed using the class name, while a reference to a non-static variable needs an instance of the class. For example, the standard output stream out is declared static in the System class, called using System.out .
Finally, consider the following program, which has a counter static variable to store the total number of instances of the Util class created so far. To demonstrate the difference between static and non-static member variables, remove the static keyword from the counter variable, and note the difference in the output:
Difference between static and non-static variables in Java
The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories:
- Static Variables: When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.
Important points for static variables :-- We can create static variables at class level only. See here
- static block and static variables are executed in the order they are present in a program.
Java
from m1 Inside static block Value of a : 20 from main
- Non-Static Variable
- Local Variables: A variable defined within a block or method or constructor is called local variable.
- These variables are created when the block in entered or the function is called and destroyed after exiting from the block or when the call returns from the function.
- The scope of these variables exists only within the block in which the variable is declared. i.e. we can access this variable only within that block.
- Initialisation of Local Variable is Mandatory.
Java
// if we try to call non-static method without using instance from static method it will get error || Non-static method ‘getName()’ cannot be referenced from a static context
x : 1, y: 10 x : 100, y: 10 x : 1000, y: 2000
- Instance Variables:Instance variables are non-static variables and are declared in a class outside any method, constructor or block.
- As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
- Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
- Initialisation of Instance Variable is not Mandatory. Its default value is 0
- Instance Variable can be accessed only by creating objects.
Java
Student Name: Monica Student Division: B Student Age: 14
The main differences between static and non static variables are:
Static variable Non static variable Static variables can be accessed using class name Non static variables can be accessed using instance of a class Static variables can be accessed by static and non static methods Non static variables cannot be accessed inside a static method. Static variables reduce the amount of memory used by a program. Non static variables do not reduce the amount of memory used by a program In Static variable Memory is allocated only once, at the time of class loading. In non Static variable Memory is allocated each time an instance of the class is created. Static variables Can be accessed from any part of the program. Non Static variables Can be accessed only within the class or its instance. Static variables Exists for the entire lifetime of the program. Non Static variables Exists for the lifetime of the object. Static variables Default value is assigned automatically. Non Static variables Default value is not assigned automatically. Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class. Java static and non-static variables in the same class
I need some help clarifying static and non-static variables. It is my understanding that static variables have the same value across all instances of a class. However, suppose I have a mix of static and non-static variables in the same class. When I reference a static variable, regardless of which instance is used, I will get the same value? Yet when I reference a non-static variable I will get the value associated with that particular class? That seems like a memory management nightmare. Is this really how it works and how is static memory handled? Are multiple copies of the variable created in each instance and then somehow sync’ed or is a reference by address created in each instance? Are there any pitfalls using a mix of static and non-static variables in the same class? TIA.
When I reference a static variable, regardless of which instance is used -> you do not need (and shall not) use an instance to get the value of the static variable. You should rather get its value with this syntax : YourClass.YOUR_STATIC_FINAL_VARIABLE for example. As you can see, there is no need for new YourClass().something .
Thanks but that reply is not what I am doing. I use ‘myclass1=new MyClass(); myclass2=new MyClass();’. Then I reference the static variable as ‘myclass1.something’ or ‘myclass2.something’ and expect them to have the same value. Meanwhile, ‘myclass1.somethingelse’ and ‘myclass2.somethingelse’ would have different values. That is what I am trying to verify and to know if there are any pitfalls doing that.
4 Answers 4
You are correct in your assertion . sort-of. Statics aren’t really shared between instances of the class because you don’t need a class instance to reference one.
can be accessed without an instance of Foo
String localString = Foo.MYSTRING;
I wouldn’t really worry much about how the JVM chooses to store the memory references for the static variables, that is an implementation detail that is delegated to the JVM designer.
There aren’t any pitfalls to having a class that defines both static and non-static (instance) variables, it happens all the time and is perfectly natural . as long as you understand how statics work, which it seems that you do.
STATIC VARIABLES
static variables belong to a class. They are common to all instances of a class and aren’t unique to any instance of a class. static attributes exist independently of any instances of a class and may be accessed even when no instances of the class have been created. You can compare a static variable with a shared variable. A static variable is shared by all of the objects of a class.
STATIC METHODS
Static methods aren’t associated with objects and can’t use any of the instance variables of a class. You can define static methods to access or manipulate static variables
You can also use static methods to define utility methods, which are methods that usually manipulate the method parameters to compute and return an appropriate value
The non-private static variables and methods are inherited by derived classes. The static members aren’t involved in runtime polymorphism. You can’t override the static members in a derived class, but you can redefine them. Any discussion of static methods and their behavior can be quite confusing if you aren’t aware of inheritance and derived classes.
- Local Variables: A variable defined within a block or method or constructor is called local variable.