0% found this document useful (0 votes)
2 views

Variable

The document explains the concept of variables in Java, detailing three types: local, instance, and static variables. Local variables are confined to the method they are declared in, instance variables are tied to class instances, and static variables are shared across all instances of a class. It also highlights the differences between instance and static variables regarding their lifecycle and accessibility.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Variable

The document explains the concept of variables in Java, detailing three types: local, instance, and static variables. Local variables are confined to the method they are declared in, instance variables are tied to class instances, and static variables are shared across all instances of a class. It also highlights the differences between instance and static variables regarding their lifecycle and accessibility.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Variable

A variable is a container which holds the value while the program is executed. A variable
is assigned with a data type.Variable is a name of memory location.

There are three types of variables in java:

local

instance

static.

Local Variable:

A variable declared inside the body of the method is called local variable. You can use
this variable only within that method and the other methods in the class aren't even aware
that the variable exists.

A local variable cannot be defined with "static" keyword.

Instance Variable:

A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.

It is called an instance variable because its value is instance-specific and is not shared among
instances.

Static:

A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the
memory.

Example:

public class staticMethod {


static int b = 5;// static variable
int c = 10;// instance variable(not static)
void fun1() {
int a = 5; // local variable
System.out.println("a=" + a + ", " + "b=" + b + ", " + "c=" + c);
c++;
a++;
b++;
}
public static void main(String[] args) {
int i = 0;// instance variable
staticMethod s = new staticMethod();
while (i <= 3) {
s.fun1();
i++;
}
System.out.println(staticMethod.b);//Static variables are created when
the program starts and destroyed when the program stops.
//Static variables can be accessed by calling with the class name
ClassName.VariableName.
}
}

OutPut:

What is the difference between class variables and instance variables in Java?
Instance variable:
1) Instance variables are created when an object is created with the use of the keyword
'new' and destroyed when the object is destroyed.
2) Instance variables can be accessed directly by calling the variable name inside the
class. However, within static methods (when instance variables are given accessibility),
they should be called using the fully qualified name. ObjectReference.VariableName.
Static variable:
1) Static variables are created when the program starts and destroyed when the program
stops.
2) Static variables can be accessed by calling with the classname ClassName.VariableName.

You might also like