Skip to main content

Difference Between Static and non-Static Variable in Java

Difference Between Static and non-Static Variable in Java

The variable of any class are classified into two types;
  • Static or class variable
  • Non-static or instance variable

Static variable in Java

Memory for static variable is created only one in the program at the time of loading of class. These variables are preceded by static keyword. tatic variable can access with class reference.

Non-static variable in Java

Memory for non-static variable is created at the time of create an object of class. These variable should not be preceded by any static keyword Example: These variables can access with object reference.

Difference between non-static and static variable

Non-static variableStatic variable
1These variable should not be preceded by any static keyword Example:
class  A
{
int a;
}
These variables are preceded by static keyword.

Example

class  A
{
static int b;
}
2Memory is allocated for these variable whenever an object is createdMemory is allocated for these variable at the time of loading of the class.
3Memory is allocated multiple time whenever a new object is created.Memory is allocated for these variable only once in the program.
4Non-static variable also known as instance variable while because memory is allocated whenever instance is created.Memory is allocated at the time of loading of class so that these are also known as class variable.
5Non-static variable are specific to an objectStatic variable are common for every object that means there memory location can be sharable by every object reference or same class.
6Non-static variable can access with object reference.

Syntax

obj_ref.variable_name
Static variable can access with class reference.

Syntax

class_name.variable_name
Note: static variable not only can be access with class reference but also some time it can be accessed with object reference.

Example of static and non-static variable.

Example

class Student
{
int roll_no;
float marks;
String name;
static String College_Name="ITM";
}
class StaticDemo
{
public static void main(String args[])
{
Student s1=new Student();
s1.roll_no=100;
s1.marks=65.8f;
s1.name="abcd";
System.out.println(s1.roll_no);
System.out.println(s1.marks);
System.out.println(s1.name);
System.out.println(Student.College_Name); 
//or System.out.println(s1. College_Name); but first is use in real time.
Student  s2=new  Student();
s2.roll_no=200;
s2.marks=75.8f;
s2.name="zyx";
System.out.println(s2.roll_no);
System.out.println(s2.marks);
System.out.println(s2.name);
System.out.println(Student.College_Name); 
}
}

Output

100
65.8
abcd
ITM
200
75.8
zyx
ITM
Static and non-Static Variable
Note: In the above example College_Name variable is commonly sharable by both S1 and S2 objects.

Understand static and non-static variable using counter

Program of counter without static variable

In this example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.

Example

class Counter
{
int count=0;//will get memory when instance is created  
Counter()
{
count++;  
System.out.println(count);  
}  
public static void main(String args[])
{  
Counter c1=new Counter();  
Counter c2=new Counter();  
Counter c3=new Counter();  
}
}  

Output

1
1
1

Program of counter by static variable

As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.

Example

class Counter
{
static int count=0; //will get memory only once  
Counter()
{
count++;  
System.out.println(count);  
}  
public static void main(String args[])
{
Counter c1=new Counter();  
Counter c2=new Counter();  
Counter c3=new Counter();  
}
}

Output

1
2
3

Comments

Popular posts from this blog

C++ Loops (For, While and Do while)

                                                   The for statement The C++ for statement has the following form: Syntax: for  (expression1;Condition;expression2) statement; for  (expression1;Condition;expression2) { block of statements } expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment); NOTE: C/C++ basically treats for statements as while type loops For loop example program: /* Example Program For for Loop In C++ Programming Language */ // Header Files #include<iostream> using namespace std; //Main Function int main() { // Variable Declaration int x=3; //for loop for (x=3;x>0;x--) { cout<<"x="<<x<<endl; } //Main Function return Statement return 0; } Output: x=3 x=2 x=1 ...

Introduction To C++

       Lets learn The C++ Programming Language C++ ranks 4th in popularity according to 2016 IEEE spectrum Top Programming Language ranking. Learning C++ is a wise investment for all programmers.                    What is C++? “C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.” In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Srroutrup in 1979. Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today. Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.                             ...

Conditional structure: if and else

                                                 Conditional structure: if and else The if statement executes based test expression inside the braces. If statement expression is to true, If body statements are executed and Else body statements are skipped. If statement expression is to false If body statements are skipped and Else body statements are executed. Simply, Block will execute based on If the condition is true or not. IF conditional statement is a feature of this programming language which performs different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch prediction, this is always achieved by selectively altering the control flow based on some condition. if and else Syntax if (expression) // Body will execute if expression is true or non-zero { //...