Static Variable in Java Language
The word static means, lack in movement or staying in one
place without any movement. For example, we have 365 days in a year.
We also have this concept in java language, known as static
keyword. This static keyword can be used with variables, methods etc.
In order to define a variable as static in Java language, we
have to use static keyword before variable. When a variable is declared as
static, then it means that only a single copy of that variable is made and then
shared with all the objects of that class.
When a variable is made as static variable in a Java class,
so whenever user creates objects of that class, all the objects will have just
one place of static variable which is shared among them.
We can also say that static variables are global variables.
If a programmer wants to create a common property for all
instances of a class, in that case they can use static variables
Static variable will get space in memory only one time
If we want to access them, we can directly use them with
class name because there is no need to create object first and access them
using object
Syntax for Static variable in Java
className.variableName
For Example
If we have a variable x in a class having initial value 0
int x = 0;
and inside constructor, we increment the value of x by 1 using ++ operator.
When we create first object of a class, in that case a copy of variable x is created for the first object and value will be incremented by 1 because of constructor. This will have a separate section for object 1.
When we create another object of the same class, in that case
one more copy of variable x is created for the second object and again value of
x will be incremented to 1 because of constructor.
In both cases, we will get answer 1, because separate copies
of x are created for both the objects.
For Example
public class StaticExample
{
int x=0;
{
x++;
System.out.println(x);
}
public static void main(String[] ar)
{
StaticExample obj1 = new StaticExample();// here answer will
be 1
StaticExample obj2 = new StaticExample();// here answer will
be again 1
}
}
{
static int x=0; //x is a static variable now
StaticExample //constructor
{
x++; //increment by 1
System.out.println(x);
}
{
StaticExample obj1 = new StaticExample();// answer will be 1
because x is static variable
StaticExample obj2 = new StaticExample();// answer will be 2 because x is static variable
}
}
Instance variable in Java
Instance variables are those variables in java that are
declared inside class.
When an object of a class is created than a separate copy of
each instance variable is created for every object and that is why they are
known as instance variables.
Syntax
class Test
{
int age; // age is instance variable
}
When an object of the class is created, at that time instance variables are created.
Instance variables are created in class but outside method body.
We can use access modifiers as well for instance variables.
They are visible inside the class and can be used or call directly with their name.
They also have their default values depending on the date type. for example, for int, it has value 0 as default
For Example
public class InstanceVariable{
public String SubjectName; // Instance variable and visible to others because of public
private int x;// Instance variable and visible only in Current Class because of private
public InstanceVariable(String name)
{
SubjectName =
name;
}
public void
forX(int y)
{
x = y;
}
public void show()
{
System.out.println(SubjectName ); // show result of SubjectName
System.out.println(x); // show result of variable x
}
public static void
main(String args[])
{
InstanceVariable
obj = new InstanceVariable("Java");
obj.forX(5);
obj.show();
}
}
Output:
Java
5
Reference Variable in Java
Any variable in Java that is used to hold the reference of
some object and not the primitive data types is known as reference variable.
If a variable holds the value of primitive data types e.g int, float etc, then it is not reference variable, it is known as Non reference variable.
In Java, String is actually not a data type, its an object
and when we create some String type variable in Java and assign some value, its
actually a reference variable.
For Example
String name = "Java";
here, name is a reference variable because it has String type
Another example can be related to User defined classes. When user creates a class in Java, and when he tries to create object of that class. In that case we have reference variable to hold the reference of an object.
For example
class Test
{
...
}
Test obj = new Test();
here in above example, obj is a reference variable.
If we use some primitive data type in Java, then it will not be a reference variable, it will be Non reference variable
For example
int number = 1;
here number is a Non reference variable
Reference variables can be make using Java built in classes or user defined classes.
For example, in Java, we have String class, Arrays class, Math class etc. We can create reference variables of these classes to call their members.
Similarly, we can create reference variables of user defined classes.
For example
...
....
}
Abc x = new Abc();
x.memberfunctionName();
Example Program
class A
{
public void ftn(String SubjectName)
{
System.out.println("Subject name " + SubjectName);
}
}
public class B
{
public static void main(String[] ar)
{
A obj = new
A(); //obj is a reference variable to hold a class object
obj.ftn("Java");
}
}
Output:
Subject name Java

No comments:
Post a Comment