DEV Community

Roshan Shambharkar
Roshan Shambharkar

Posted on

Types of Variables in Java

In Java we have three types of variable called instance, local and static

Note:- In Java we didn't support global variables

Instance variables

Instance variables are those variables which we define in the class out not in method

public class A {
   String name;
   String color;
}

Enter fullscreen mode Exit fullscreen mode

in the above example in class A we defined some variables using data type in the above code snippet we had define instance variables inside the class but out side the method these variables are instance variables if you want access instance variables outside the class then you have to use access specifier public instance variables scope depended upon access specifier

Local variables

A variables used while method declaration passing method augments or parameter or declaring variables in side method body called as local variables

public int caluclate(int a, int b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

In above code snippet variables declare in method called as local variables you can only use this variable only inside the method

*Static variables *

static variables defined using static keyword after declaring static static keyword in front of your variable the variables called as static variables

public class B {
  static String name = "Ryan";
}
Enter fullscreen mode Exit fullscreen mode

The above code snippet is a example of static variables

Top comments (0)