Types of Variables in Java with Examples





Hey guys in this quick post, we will discuss different types of variables in Java programming with examples

Overview


Variables is just a placeholder or container to store the data, it can be changed during the program execution.

There are 3 different types of variables:

  1. Instance variables
  2. Static variables
  3. Local variables

Instance variables


  • Instance variables are declared inside the class but outside the method
public class Test {
	
	int a = 10;
	
	public void m1() {
		//TODO
	}
}
  • Instance variables cannot be accessed directly inside the static method or block
public class Test {
	
	int a = 10;
	
	public static void main(String[] args) {
		System.out.println(a); //compilation error
	}
}
  • We can access the instance variables inside static method using Object reference
public class Test {
	
	int a = 10;
	
	public static void main(String[] args) {
		Test t = new Test();
		System.out.println(t.a); //10
	}
}
  • Initialization is optional for instance variables, JVM will provide the default values
public class Test {
	
	int a;
	String b;
	boolean c;
	
	public static void main(String[] args) {
		Test t = new Test();
		System.out.println(t.a); //0
		System.out.println(t.b); //null
		System.out.println(t.c); //false
	}
}

Static variables


  • Static variables are also called as class level variables. Hence we cannot create static variables inside methods
public class Test {
	
	public void m1() {
		static int a = 10; //compilation error
		
	}
}
  • It creates a single copy for every object and that copy will be shared by all objects of that class
public class Test {
	static int a = 10;
	public static void main(String[] args) {
		Test t1 = new Test();
		System.out.println(t1.a); //10
		Test t2 = new Test();
		System.out.println(t2.a); //10
	}
}
  • Static variables can be accessed directly by using class name
public class Test {
	static int a = 10;
	public static void main(String[] args) {
		System.out.println(Test.a); //10
	}
}
  • Just like instance variables, initialization is optional for static variables as well, JVM will provide the default values
public class Test {
	static int a;
	static boolean b;
	static String c;
	public static void main(String[] args) {
		System.out.println(Test.a); //0
		System.out.println(Test.b); //false
		System.out.println(Test.c); //null
	}
}

Local variables


  • Local variables are also called as temporary variables
  • Local variables are created inside the method or block or constructor
public class Test {
	public Test() {
		int b = 10;
	}
	public void m1() {
		int a = 10;
		
	}
}
  • The scope of the local variables is exactly same as the block in which we declared it.
public class Test {
	public static void main(String[] args) {
		int sum = 0;
		for (int i = 0; i < 5; i++) {
			sum = sum + i;
		}
		System.out.println(sum);
		System.out.println(i); //compilation error
	}
}
  • For the local variables, we should initialize the default values before using it
public class Test {
	public static void main(String[] args) {
		int a;
		System.out.println(a); //compilation error
		a = 10;
		System.out.println(a); //10
	}
}

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



Partners: 5 euro paypal casino seriösen casino ohne lizenz Bingo Bonga Casino trustly casinos astropay casino online zimpler betalning sweet bonanza echtgeld rahalaitos huijaus utländska casinon med trustly online casino auszahlung ohne ausweis

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply