Variables in Java

Variables allow a java program to store data during the runtime of the program. Variable is just the name of the memory location.
int count = 10;

Variables in Java - Java Point Tutorial

Sometimes variables are also referred to as fields. There are three types of fields in Java.

Types of Variables in Java

  • Local Variables

  • Static Variables

  • Instance Variables (non-static fields)

Types of Variables in Java | Local Variables in Java | Static Variables in Java | Instance Variables in Java

A variable can either be a primitive type that can store values int, float, double, Boolean and char or reference type variable that can store the memory address of a memory location. Arrays and objects are reference data type variables.
Variables can be defined as

int count = 10;
long l = 25;
String name = “JAVA”;

Local Variables in Java with Example

A variable that I declared inside a method or a block is called Local variable. Local variables are not accessed outside the block or method.
For Example –

public double area() {
		double area = 0; // declaration of local variables
		area = PI * radius * radius;
		return area;
	}

Important points about local variables:

  • Local variable declaration can’t have access modifier.

  • The final is the only modifier available to local variables. The final modifier used to define constants. The final variable value can’t be changed after the declaration.

  • The local variable doesn’t get default values so it is necessary to initialize a local variable while declaring.

Static Variables in Java with Example

A class variable is a field declared with the static modifier. A class variable is the part of a class not to object, that means class variables get the memory as soon as class loads, object creation is not necessary. static modifier tells JVM to allocate memory only once in the life of the static variable, regardless of how many times the class has been instantiated.

public class Circle {

	private int radius; // instance variable
	public static double PI = 3.14; // static variable

	public double area() {
		double area = 0; // declaration of local variables
		area = PI * radius * radius;
		return area;
	}

In this example, JVM will allocate memory to PI only once in life whether n number of instances created for the Circle class.

Circle c1 = new Circle();								
Circle c2 = new Circle();								
Circle c3 = new Circle();

Class variables can be accessed with objects or class name.

double d = Circle.PI; // legal statement
double d = c1.PI; // legal statement

Instance Variables in Java (non-static field) with Example

Instance variables are associated with an instance of the class (also called object). A variable that is declared inside the class but outside the method is called the Instance variable. The state of an object is stored in instance variables. It is not declared as static.

Important points about instance variables

  • Instance variables can have access modifier.

  • It can be defined as final or transient.

Example to understand the types of variables in java

class mydemo{  
int data=100;//instance variable  
static int m=70;//static variable  
void method(){  
int n=9;//local variable  
}  
}//end of class  

Display Variables

The println() method is often used to display variables.

To combine both text and a variable, use the + character:

public class mytutorial {
  public static void main(String[] args) {
    String z = "Dhiren";
    System.out.println("Hello " + z);
  }
}
Example Output

You can also use the + character to add a variable to another variable:

public class mytest {
  public static void main(String[] args) {
    String x= "Hello ";
    String y = "Java Point Tutorial";
    String z = x + y;
    System.out.println(z);  
  }
}
Example Output

For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):

class Mydemo{  
public static void main(String[] args){  
int x=50;  
int y=50;  
int z=x+y;  
System.out.println(z);  
}}
Example Output

Java Variable Example: Widening

class Demotest{  
public static void main(String[] args){  
int X=50;  
float Z=X;  
System.out.println(X);  
System.out.println(Z);  
}}
Example Output

Share