Java Variable Types - Types of Variables in Java



Java Variable Types - Types of Variables in Java

  • There are three types of variables in Java
    • Local Variable
    • Instance variable
    • Static Variable

Local Variable

  • A variable which is declared inside the body of the method is called Local Variable.
  • This variable can be used only inside the function where it is declared.
  • This type of variable should not be declared with a static keyword.

Instance Variable

  • A variable which is declared inside the class and outside a body of method is called as instance variable.
  • This type of variable cannot be declared as static.
  • It is called an instance variable because its value is instance-specific and is not shared among instances.

Static Variable

  • A variable that is declared with a static keyword is called as static variable.
  • Static variables cannot be declared as local variables.
  • 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.

Examples for Variables in Java

public class kaashiv {
	
	static int m=100;//static variable  
    void method()  
    {    
        int n=90;//local variable    
    }  
	public static void main(String[] args) {
		int data=50;//Instance variable   
	}}

Related Searches to Java Variable Types - Types of Variables in Java