Java Variables - Local, Static and Instance Variable in Java

Post a Comment

The term variable is the combination of vari and able which means that we can change its values. The reserved area that is allocated in memory is known as variable and in simple words, the name of memory location is known as variable

Java Variables - Local, Static variable in java and Instance Variable in Java

Java Variable

In Java, variables is a place which holds the value during the execution of Java programs. And each variable is assigned with a data type. 

In java, there are three types of variables;

• Local

• Static

• Instance

Now, let's discuss these variables in detail.

Local Variable

Local variables are those variables which are declared inside the body of the method. In simple words we can say that local variable can be used by programmers only within the method and other methods will not be aware of its existence. And we can't define a local variable with static keyword.

Static Variable

Static variables are those variables which are declared as Static. Only during the loading of class memory allocation of static variable is possible. Static variable can't be local variable. You can share the static variable among the instance of the class by creating it's single copy. 

Instance Variable

Instance variables are those variables which are declared outside the method but inside the class. The value of this variable is instance specific that's why it is known as instance variable. 


Some examples of Java Variables are;

public class A

{

static int m=100;//static variable

void method()

{

int n=90;//local variable

}

public static void main(String args[])

{

int data=50;//instance variable

}

}//end of class


• Add two numbers - Java variable example

public class Simple{

public static void main(String[] args){

int a=50;

int b=50;

int c=a+b;

System.out.println(c);

}

}

Output

100

• Widening - Java variable example

public class Simple{

public static void main(String[] args){

int a=50;

float f=a;

System.out.println(a);

System.out.println(f);

}}

Output

50

50.0

• Narrowing (Typecasting) - Java variable example

public class Simple{

public static void main(String[] args){

float f=50.5f;

//int a=f;//Compile time error

int a=(int)f;

System.out.println(f);

System.out.println(a);

}}

Output

50.5

50

• Overflow - Java variable example

class Simple{

public static void main(String[] args){

//Overflow

int a=150;

byte b=(byte)a;

System.out.println(a);

System.out.println(b);

}}

Output

150

-106

• Adding Lower Type - Java variable example

class Simple{

public static void main(String[] args){

byte a=50;

byte b=50;

//byte c=a+b;//Compile Time Error: because a+b=20 will be int

byte c=(byte)(a+b);

System.out.println(c);

}}

Output

100


String - text are stored and written within the double quotes such as "Hello".

int - integers are stored without decimals like 22.

float - floating point numbers are stored including decimals like 2.55.

char - single characters are stored and written within double quotes such as "A".

boolean - value with two states are stored like yes or no.


Related Posts

Post a Comment