To
manipulate data, we have to reserve some space in memory and to access that
memory space, that memory space should have some name. Variables names are
memory blocks name, where data is stored. Data type of a variable defines what
kind of data and what range of value can be stored in a variable.
Syntax of declaring a variable in java:
data-type variable-name;
We can also initialize when declaring a variable. For example:
int a = 5;
Types of Variables in Java
- Local Variable
- Instance Variable
- Class/Static Variable
Local Variable
Local variables can only be locally accessed. Local variables
are declared in blocks, methods, constructors etc. Life time of a local
variable is local to a block in which it is defined. Suppose a variable i is declared in a method,
when this method is executing variable i reserves space in memory and destroys
when this methods completes execution.
We can not give any access modifier to a local variable.
Programming Example of Local Variable in Java
public class LocalVariable
{
void sum(int x, int y)
{
int a =x, b = y; //Local Variables
int c = a + b; //Local Variable
System.out.println("Sum of "+a+" and
"+b+" is "+c);
}
public static void main(String args[])
{
LocalVariable L = new LocalVariable();
L.sum(1, 3);
}
}
OUTPUT:
Sum of 1 and 3 is 4
Instance Variables
Instance variables are declared inside a class and outside all
methods, constructors, or any bock. When an object is created, instance
variables occupies space in heap memory. And when object destroys, instance
variables also destroys. We can also give any access modifier to an instance
variable. All methods, constructors or any block can access instance
variables. It is a good practice to make instance variables private, so they
are private to other classes.
Programming Example of Instance Variable in Java
public class InstanceVariable
{
private int
roll_no;
public String
name;
public
InstanceVariable(String n)
{
name
= n;
}
public void
setRoll_no(int r)
{
roll_no
= r;
}
public void
showDetail()
{
System.out.println("Name
: "+name+"\nRoll no. : "+roll_no);
}
public static void main(String args[])
{
InstanceVariable
I = new InstanceVariable("John");
I.setRoll_no(32);
I.showDetail();
}
}
OUTPUT:
Name : John
Roll no. : 32
Class/Static Variable
Static
variables are declared outside all instance member methods, constructors or any block with a
static keyword. Static variables are also known as class variables. Static
variable does not depends on object of a class. There is only one copy of a
static variable for a class.
Static variable loads into memory when class is loading into JVM and removes from memory when class is removing from JVM.
Static variable loads into memory when class is loading into JVM and removes from memory when class is removing from JVM.
Static
variables can be accessed with the class name,
Syntax:
class-name.variable-name;
Programming Example of Instance Variable in Java
public class StaticVariable
{
public static String
course;
private int roll_no;
public String name;
public
StaticVariable(String n)
{
name =
n;
}
public void
setRoll_no(int r)
{
roll_no = r;
}
public void
showDetail()
{
System.out.println("Name
: "+name+"\nRoll no. : "+roll_no+"\nCourse :
"+course);
}
public static void main(String args[])
{
StaticVariable
I = new StaticVariable("John");
I.setRoll_no(32);
StaticVariable.course
= "JAVA";
I.showDetail();
}
}
OUTPUT:
Name : John
Roll no. : 32
Course : JAVA
0 comments:
Post a Comment