JAVA-Variable Types
Introduction to Java variables
3 min readJun 14, 2021
Java variables provide us with named storage and that can manipulate our programs. Each variable has a specific type as well and it determines the size and layout of the variable’s memory. A set of operations can be applied to the variable and the range of values will be stored in the variable’s memory.
There are three types of variables available in Java.
- Local variables
- Instance variables
- Class/Static Variables
Local Variables
- These variables are declared in methods, blocks or constructors.
- Created when the constructor, method or block is entered.
- Access modifiers cannot be used for this kind of variables and these are implemented at the stack level internally.
- Local variables should be declared and an initial value should be assigned before the first use since there is no default value for these variables.
Example:
public class Test1{ public void myAge(){
int age = 0;
age= age+7;
System.out.pritnln("My age is :" + age);
}public static void main (String args[]){Test1 test1 = new Test1();
test1.myAge();
}}//My age is: 7
Instance Variables
- These variables are declared in a class, but outside a constructor, method or any block.
- When the object is created these variables are created. When we creating an object we use the “new” keyword. Once this new keyword destroyed the object is also destroyed.
- These variables declared in a class level before or after use.
- These are having default values. As an example for numbers default value is 0, for boolean it's false.
- Can be accessed directly using the variable name which is used inside the class.
Example:
import java.io.*;public class Student{ // this instance variable is visible for any child class.
public String name; // name variable is visible in Student class only.
private double Marks; // The name variable is assigned in the constructor.
public Student(String stuName){
name = stuName;
} // The marks variable is assigned a value.
public void setMarks(double stuMarks)
{ marks= stuMarks;
} // This method prints the employee details.
public void printStu(){
System.out.println("name : " + name );
System.out.println("marks :" + marks);
}public static void main(String args[]){
Student stuOne = new Student("Maleesha");
stuOne.setMarks(1000);
stuOne.printStu();
}
}
Class/Static Variables
- These variables declared with the “static” keyword in a class, but outside a method, block or constructor.
- These variables are declared as constants. Constant variables never change their initial values.
- Stored in static memory and rarely use these variables other than declared final and used as either public/private constants,
- These variables created when the program starts to running and destroys when it stopped.
- Can be accessed by calling with the class name ClassName.VariableName.
Example:
import java.io.*;
public class Emp{ // salary variable is a private static variable
private static double salary; // DEPARTMENT is a constant
public static final String DEPARTMENT = "Fashion Design";
public static void main(String args[]){ salary = 120000;
System.out.println(DEPARTMENT + "average salary:" + salary); }
}
//Fashion Design average salary:120000