Definition of a variable?
- A variable is one place where the program data is stored temporarily.
- The variable is the basic unit of memory storage in a Java program
How to create a java variable?
- (data type) (name of the variable) = (value of variable) ( ; )
- example 1: int a = 10 ;
- example 2: int a ;
- In the above example, (1) value of the java variable is 10 and call as initializing a java variable of a
- The second example in the java variable of value does not initialize.
There are three types of java variable
- local variable
- instance variable
- static variable
1) Local variable
- The local variable declared in the body of the method is called the local variable
- it variable is visible only for in a specific part like method, loop, constructor, block, etc
- when you exit method, constructor, loop, block that time the variable destroyed
- The local variable is not in its own default value
- This variable in access only in a specific method, block, etc
Example 1:
public class Localvariable {
public void sum() {
//using local variable inside of the body
int a = 10;
int b = 20;
int ans= a+b;
System.out.println("sum of a and b = "+ans);
}
public static void main(String[] args) {
Localvariable l1= new Localvariable();
l1.sum();
}
}
output
sum of a and b = 30
Example 2:
public class Localvariable {
public void sum() {
//local variable outside of method
int a = 10;
int b = 20;
int ans= a+b;
}
public static void main(String[] args) {
//compile time error show
System.out.println("sum of a and b = "+ans);
}
}
compile-time error
ans(variable) can not be resolved to the error
In the above two examples show that the first example is complete work but when put in local variable outside the method that time compile-time error
2) Instance variable
- Instance variable is outside of the method, constructor, block and inside of the class but not a static variable
- when the object is created that time instance variable is created and the object is destroyed that time instance variable is destroyed
- it's a class-level variable
- instance variable in use in the method, constructor, block in the class create
- this is direct access in his name and use to method block etc
- it's default value is 0(ZERO).
Example :
public class InstanceVariable {
//instance variable outside method
int a =10;
public void findvalue() {
//use this variable
System.out.println("value of a is = "+(a));
}
public static void main(String[] args) {
//create the object
InstanceVariable i1=new InstanceVariable();
i1.findvalue();
}
}
Output
Example :
output
value of a is = 10
3) static variable
- static variable is used to memory management purposes
- when creating the separate object that time static variable provides the common property of all objects
- If put static keyword above in instance variable that time its call as a static variable
- program is started that time static variable is created and program end that time all static variable is destroyed
- This variable is known as a class variable
- When calling the only static variable that time no need to create an object
- This variable initialization is mandatory
Example :
public class StaticVariable {
int enrollnumber;
String studentname;
// create the static variable
static String collagename="GTU";
//create constructor
StaticVariable(int e,String s){
enrollnumber = e;
studentname = s;
}
// create the student details method
public void studentdetails() {
System.out.println(enrollnumber +" "+studentname+" "+collagename);
}
public static void main(String[] args) {
//create two object
StaticVariable s1=new StaticVariable(1, "paul");
StaticVariable s2=new StaticVariable(2,"krish");
//call the method
s1.studentdetails();
s2.studentdetails();
}
}
1 paul GTU
2 krish GTU
- in the above example in college name is not mentioned in the object parameter because it's initialized in a static keyword above☝ and write only one time.
- so static keyword use in any common property come in repeatedly.
So it's all about in Different Type Of JAVA Variable