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

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++.                                “Hello World!” Your first C++ program will be a “Hello World!” program. You might have noticed “Hello World!” being the first program while starting

Java Basic Principles

This Blog is Specially Designed for those who can learn the theory concepts of Java (Object Oriented Language)                                                             Object Oriented programming is a programming style which is associated with the concepts like class, object, Inheritance, Encapsulation, Abstraction, Polymorphism. Most popular programming languages like Java, C++, C#, Ruby, etc. follow an object oriented programming paradigm.  As JAVA  being  the most sought-after skill, we will talk about object-oriented programming concepts in Java.   An object-based application in Java is based on declaring classes, creating objects from them and interacting between these objects.  In this blog,  we will understand the below  core concepts of  Object oriented Programming in the following sequence: Inheritance Encapsulation Abstraction Polymorphism Object Oriented Programming : Inheritance In OOP, computer programs are designed in such a way where everything is an

C++ Operators

                                                               Operators Once introduced to variables and constants, we can begin to operate with them by using operators. What follows is a complete list of operators. At this point, it is likely not necessary to know all of them, but they are all listed here to also serve as reference. Assignment operator (=) The assignment operator assigns a value to a variable.   x = 5; This statement assigns the integer value  5  to the variable  x . The assignment operation always takes place from right to left, and never the other way around:   x = y; This statement assigns to variable  x  the value contained in variable  y . The value of  x  at the moment this statement is executed is lost and replaced by the value of  y . Consider also that we are only assigning the value of  y  to  x  at the moment of the assignment operation. Therefore, if  y changes at a later moment, it will not affect the new value taken by  x . For exampl