Java is a Object Oriented Programming(OOP) language, which is often interpreted that we need objects to access methods and variables of a class, however this is not always true. While discussing static keyword in java, we learned that static members are class level and can be accessed directly without creating any instance. In this article we will discuss the difference between static and non-static members in Java.
Static Members
1. Static Fields (Variables)
- Definition: Static variables are class-level variables, which means they are shared by all instances of the class. For example, if a class has two instances obj1 and obj2, they both access to the same static variable.
- Memory Allocation: The memory is allocated to static variables when the class is loaded by the JVM.
- Access: Since, they are same for all instances, they can be called using class name without the need of any instances.
- Initialization: Initialised during memory allocation, i.e. when the class is first loaded.
Static variables Example
class MyClass {
//static variable, note the static keyword before int
static int staticVariable = 10;
}
public class Test {
public static void main(String[] args) {
// Accessed using class name, we did not create an
// instance of class as it is not needed
System.out.println(MyClass.staticVariable);
}
}
2. Static Methods
- Definition: Similar to static variables, static methods belong to class rather than any specific instance of class.
- Access: Can be accessed using class name, as shown in the following example.
- Limitations: This is very important to note, static methods cannot access non static variables as well as non static methods. This is due to the underlying nature that static methods are not tied to any instances.
Static Methods Example
class MyClass {
// Note the static keyword before return type of method
static void staticMethod() {
System.out.println("This is a Static method");
}
}
public class Test {
public static void main(String[] args) {
// Accessed using class name
MyClass.staticMethod();
}
}
Non-Static Members
1. Instance Fields (Variables)
- Definition: These variables are tied to the instances of the class.
- Memory Allocation: Memory is allocated when the new object of the class is created.
- Access: These are accessed using instances of class as shown in the example below.
- Initialization: They are initialized when the object is instantiated.
Instance Variables Example
class MyClass {
// Instance variable, there is no static keyword here
int instanceVariable = 20;
}
public class Test {
public static void main(String[] args) {
// We have to create an object of the class to access
// instance variable
MyClass obj = new MyClass();
// Accessed using object
System.out.println(obj.instanceVariable);
}
}
2. Instance Methods
- Definition: These methods are tied to the instances of the class.
- Access: These are accessed using instances of class.
- Capabilities: Unlike static methods which cannot access non-static variables and methods, these methods can access both instance variables and static variables.
Instance Methods Example
class MyClass {
// non-static variable
int instanceVariable = 20;
//non-static method
void instanceMethod() {
System.out.println("Instance method called");
}
}
public class Test {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.instanceMethod();
}
}
Key Differences between static and non-static members
- Tied:
- Static Members: Belong to the class.
- Non-Static Members: Belong to instances (objects) of the class.
- Access:
- Static Members: Can be accessed directly using class name without creating any object.
- Non-Static Members: Can be accessed using object of the class.
- Memory Allocation:
- Static Members: Memory allocation is done when the class is loaded.
- Non-Static Members: Memory allocation is done each time an object is created.
- Initialization:
- Static Members: Initialization is done when the class is loaded by JVM.
- Non-Static Members: Initialization is done every time a new object is created.
- Scope:
- Static Members: Class level scope.
- Non-Static Members: Scope is limited to the instance level.
- Access to Instance Members:
- Static Methods: Cannot access instance members directly.
- Instance Methods: Can access both instance and static members.
Static vs non-static Example Program
class Example {
// Static variable
static int staticVariable = 10;
// Instance variable
int instanceVariable = 20;
// Static method
static void staticMethod() {
System.out.println("Static method");
// Can access static variables
System.out.println(staticVariable);
// Error: Cannot access instance variables, if you
// un-comment the following statement, it will throw error
// System.out.println(instanceVariable);
}
// Non-static (Instance) method
void instanceMethod() {
System.out.println("Instance method");
// Can access static variables
System.out.println(staticVariable);
// Can access non-static variables
System.out.println(instanceVariable);
}
}
public class Test {
public static void main(String[] args) {
// Calling static method using class name
Example.staticMethod();
Example obj = new Example();
// Calling non-static(instance) method using object
obj.instanceMethod();
}
}
Prasannaraj says
Hi,
Why can’t we use in last example?
public static void main(String args[])
{
System.out.println(“Age is:”+ getAge(15));
}
praveen says
Because to access any non static members we have to first create object of that class(Here Sample is class), and then access the methods.
Now,
Public static void main()
{
Sample obj= new Sample();
Obj.setAge(30);
System.output.println(“Age=”+obj.getAge());
}
Poonam Marathe says
where the static variables are get stored?and how they get processed?
gaensh chowkekar says
static variable are stored in class common memory
meghamsh says
The Static variables are stored in heap memory….basically we use static to define a fixed final variable because it stays constant through out the application…
e.g. private static final float pi=3.14f;
akintunde says
good day, please can you explain why the error when the age isn’t assigned to an object?
laxman says
like said earlier,it is because for the non static method to be used in static, an object should be created first and then called using that obj eg. obj.getage() in static method.
hope it helps
Abhay jadhav says
Nice stuff related to java
Thank you it really helps
Haider Ali says
println is a static funtion or non- static?
plz discuss in detail.
Vidhisha says
Hi,
Can you please give example of static methods in terms of real time application.Where ,how and what is the purpose of using static methods.