Java Variables:
- Variable is a name of memory location where the value is stored to use within class, Method etc…
- Each variable in Java has a specific Datatype to store Value.
- Which decides the size and layout of the java variables memory.
- Values can be put away inside that memory, and the arrangement of activities that can be connected to the variable.
There are three different kinds of variable in Java
- Local variables
- Instance variables
- Class / Static variables
Variable Declaration:
1 2 3 4 5 6 7 8 9 10 11 12 | class Variable_Java { int instance_Var = 50;// instance variable static int static_Vat = 100;// static variable void method() { int local_Var = 90;// local variable } }// end of class |
1) Local variables
- Local java variables can be declared within methods, constructors, or blocks.
- Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
- Access modifiers cannot be used for local variables Because Local Variables defined within method.
- Local variable can be declared within the method, constructor or block.
Example:
In following example, age is a local variable which is defined inside Age() method and having its scope limited to the Age() method only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Local_Java { public void Age() { int age = 0; // Local Variable age = age + 7; System.out.println("Dog's age is : " + age); } public static void main(String args[]) { Local_Java dog = new Local_Java(); dog.Age(); } } |
Output:
1 | Dog's age is: 7 |
2) Instance variables
- Instance java variables are declared in a class, but outside a method, constructor etc.
- When a space is allocated for an object in the heap, a memory slot for each instance variable value is created.
- Instance variables are created when an object is created and destroyed when the object is destroyed.
- Instance variables hold values that must be referenced by more than one method, constructor or block.
- Access modifiers can be given for instance variables.
Example:
In following example, name is a local variable which is defined inside Employee class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package package_Name; public class Employee { // this instance variable is visible for any child class. public String name; // Instance Variable // salary variable is visible in Employee class only. private double salary; // Instance Variable // The name variable is assigned in the constructor. public Employee(String empName) { name = empName; } // The salary variable is assigned a value. public void setSalary(double empSal) { salary = empSal; } // This method prints the employee details. public void printEmp() { System.out.println("name : " + name); System.out.println("salary :" + salary); } public static void main(String args[]) { Employee emp1 = new Employee("Sandeep"); emp1.setSalary(1000); emp1.printEmp(); } } |
Output:
1 2 | name : Sandeep salary :1000.0 |
3) Class / static variables
- class java variables is also referred to as static variable and declared with the static key-word in a category, but it is outside a method, constructor or a block.
- only one replica of every class variable is created, regardless of how many objects are created.
- Static variables are stored in static memory.
- Static variables are created while the program begins and destroyed while this system stops.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Static_Java{ // salary variable is a private static variable private static double salary; // Static Variable // DEPARTMENT is a constant public static final String DEPARTMENT = "Development "; public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT+"average salary:"+salary); } } |
Output:
1 | Development average salary:1000 |