Variables in Java
There are three types of variables in Java:
Local
Instance
Static
Local Variable:
This variable is only created inside a method or block of code, in other word, the
variable created inside a method or block of code is called local variable.
The scope of this variable is within the method or the block only. This
variable is unknown outside the method or the block of code.
This variable must be assigned a value(because JVM does not
assign default value to local variable) and the value is valid only inside the
method or block where it is created.
Syntax :
Datatype variableName = variableValue; // variable value is mandatory for local variable.
Let's create a method1 and run it:
public class VariableTest {
public void method1() {
int i
= 10 ; //if you donot
assign the value for "i" , you get compile time error where ever you
try to use this variable.
System.out.println("The value of i is = "+i);
}
//Let's call
and run above method
public static void main(String[] Args) {
VariableTest obj = new VariableTest();
obj.method1();
}
}
Output:
The value of i is = 10
Let's create another method method2 inside the same class.
In method2 below, you will see compile time error when you try to use
"i" as "i" is unknown to method2.
public class VariableTest {
public void method1() {
int i
= 10 ; //if you donot
assign the value for "i" , you get compile time error where ever you
try to use this variable.
System.out.println("The value of i is = "+i);
}
public void method2() {
System.out.println("The value of i as accessed from method2 is = "+i); //This line shows compile time
error
}
//Let's call
and run above method
public static void main(String[] Args) {
VariableTest obj = new VariableTest();
obj.method1();
obj.method2();
}
}
Output:
This time, you can not execute the code as it shows compile
time error.
Instance variable
Instance variable are the variable created inside the class
but outside the method . These variables may be initialized or not but
initialization is not mandatory. When not initialized, JVM takes care of the
default value for the variable. Such as if it is "int" the default
value =0, if it is "String" the default value = null, if it is
"float" the default value = 0.0 , if it is "double" the default value = 0.0, if it is "boolean" the default value = false and so on.
Instance variable can be called anywhere in the code and can
be assigned different value as required using the class instance(object) as in
the below example .
Syntax:
dataType variableName = variableValue // variable value is not mandatory.
public class VariableTest {
int ID = 50; // instant variable initialized(=50) though initialization is
not mandatory. . If not initialized, the default value for int = 0 ;
//Let's create instance of above class and use the variable.
public static void main(String[] Args) {
VariableTest obj1 = new VariableTest();
VariableTest obj2 = new VariableTest();
obj1.ID = 60;
obj2.ID = 70;
System.out.println(" Instance1 value of ID = "+obj1.ID);
System.out.println(" Instance2 value of ID = "+obj2.ID);
}
}
Output:
Instance1
value of ID = 60
Instance2
value of ID = 70
Static Variable:
If you use
"static" word before the instance variable, it becomes static
variable, ie, static variable is inside
the class and outside the method and it has "static" word infront of
the datatype. Initialization of the variable is not mandatory.
If the variable is static, you do not need to create object of that class.
If you are writing a
variable which needs to be same through out the code , you can use static
variable. Even if you change the variable value at one point, that change will change the variable
value everywhere the class object/instance is used to define the variable value.
We will discuss with example as below.
Syntax:
static dataType
variableName = variableValue //
variable value is not mandatory.
Example1:
In this example the school
is static variable and stuID is instance variable.
public class VariableTest {
static String school = "JRSchool" ; //Static variable , initialization is not mandatory.
int stuID ; //
instant variable , initialization is not mandatory
//Let's create instance of above class and use the variables.
public static void main(String[] Args) {
VariableTest david = new VariableTest();
VariableTest jack = new VariableTest();
VariableTest linda = new VariableTest();
david.stuID = 10;
jack.stuID = 11;
linda.stuID = 12;
System.out.println("Student ID of David = "+david.stuID);
System.out.println("Student ID of Jack = "+jack.stuID);
System.out.println("Student ID of Linda = "+linda.stuID);
System.out.println("School of David = "+david.school);
System.out.println("School of Jack = "+jack.school);
System.out.println("School of Linda = "+linda.school);
}
}
Output:
Student
ID of David = 10
Student
ID of Jack = 11
Student
ID of Linda = 12
School
of David = JRSchool
School
of Jack = JRSchool
School of Linda = JRSchool
Now, let's change
the school and stuID of Linda.
public class VariableTest {
static String school = "JRSchool" ; // We will change this values below for Linda.
int stuID ; // We will change this values below for Linda.
//Let's create instance of above class and use the variables.
public static void main(String[] Args) {
VariableTest david = new VariableTest();
VariableTest jack = new VariableTest();
VariableTest linda = new VariableTest();
david.stuID = 10;
jack.stuID = 11;
linda.stuID = 5; // value changed
linda.school = "SRSchool"; // value changed
System.out.println("Student ID of David = "+david.stuID);
System.out.println("Student ID of Jack = "+jack.stuID);
System.out.println("Student ID of Linda = "+linda.stuID);
System.out.println("School of David = "+david.school);
System.out.println("School of Jack = "+jack.school);
System.out.println("School of Linda = "+linda.school);
}
}
Output:
Student
ID of David = 10
Student
ID of Jack = 11
Student
ID of Linda = 5
School
of David = SRSchool
School
of Jack = SRSchool
School of Linda = SRSchool
As you can see above, in
the output ,the value of "stuID" has been changed for Linda only as
it is an instance variable.
However, the value of "school"
changed for all the students though we changed the value for Linda
only, this is because "school" is static variable.
If the variable is
static, you do not need to create object/instance of the class:
public class VariableTest {
static int ID = 50; // static variable
initialized(=50) though initialization is not mandatory. If not initialized,
the default value for int = 0 ;
public static void main(String[] Args) {
System.out.println(" The value of static variable ID = "+VariableTest.ID);
}
}
Output:
The value of static
variable ID = 50
No comments:
Post a Comment