Local Variable
A local variable belongs to its enclosing method or block. Assign it a value before reading it.
A Java variable has a type and can hold a primitive value or a reference. Its declaration determines its scope and lifetime.
Change a stored number
class Main {
public static void main(String[] args) throws Exception {
int n = 2;
n++;
System.out.println(n);
}
}
3
A student buys three notebooks at 125 cents each. Add the 50-cent delivery charge once for the whole order. Keeping the arithmetic in cents makes the total easy to check against the receipt.
A local variable belongs to its enclosing method or block. Assign it a value before reading it.
Initialize the local variable str and print it.
class Operators {
public static void main(String[] args) {
// Declaration of a variable
String str;
// Initialization of a variable
str = "Hello World";
// Print the value of a Variable 'str'
System.out.println(str);
}
}
Hello World
Initialize the final variable and print its value.
class Operators {
public static void main(String[] args) {
// Declaration and Initialization of a variable
String str = "Hello World";
// Print the value of a Variable 'str'
System.out.println(str);
}
}
Hello World
Follow the inner and outer blocks to see where the local variable can be used.
class Operators {
public static void main(String[] args) {
{ // Inner Scope starts
// Declaration of a variable - Inner Scope
String str;
// Initialization of a variable
str = "Hello";
// Print the value of a Variable 'str'
System.out.println(str);
} // Inner Scope ends
// Declaration of a variable - Outer Scope
String str;
// Initialization of a variable
str = "World";
// Print the value of a Variable 'str'
System.out.println(str);
}
}
Hello World
Inspect the compiler diagnostic to identify the overlapping local declarations.
class Operators {
public static void main(String[] args) {
// Declaration of a variable - Outer Scope
String str;
// Initialization of a variable
str = "World";
// Print the value of a Variable 'str'
System.out.println(str);
{ // Inner Scope starts
// Declaration of a variable - Inner Scope
String str;
// Initialization of a variable
str = "Hello";
// Print the value of a Variable 'str'
System.out.println(str);
} // Inner Scope ends
}
}
HelloWorld.java:16: error: variable str is already defined in method main(String[]) String str;
An instance field belongs to an object. Each instance has its own field value.
Assign str through an object reference, then print the field.
class HelloWorld {
// Instance variable declaration
private String str;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'HelloWorld'
HelloWorld obj = new HelloWorld();
// Initialization of an instance variable 'str' through local variable 'obj'
obj.str = "Hello World";
// Print the value of an instance variable 'str' through local variable 'obj'
System.out.println(obj.str);
}
}
Hello World
Create two objects and assign a different value to each object's field.
class HelloWorld {
// Instance variable declaration
private String str;
public static void main(String[] args) {
// Declaration of a variable 'obj1' and 'obj2' of type 'HelloWorld'
HelloWorld obj1 = new HelloWorld();
HelloWorld obj2 = new HelloWorld();
// Initialization of an instance variable 'str' through local variable 'obj1'
obj1.str = "Hello";
// Initialization of an instance variable 'str' through local variable 'obj2'
obj2.str = "World";
// Print the value of an instance variable 'str' through local variable 'obj1'
System.out.println(obj1.str);
// Print the value of an instance variable 'str' through local variable 'obj2'
System.out.println(obj2.str);
}
}
Hello World
Initialize the instance field in its declaration, then read it from an object.
class HelloWorld {
// Instance variable declaration and Initialization
private String str = "Hello World";
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'HelloWorld'
HelloWorld obj = new HelloWorld();
// Print the value of an instance variable 'str' through local variable 'obj'
System.out.println(obj.str);
}
}
Hello World
A static field belongs to the class. All instances share the same field. Refer to it by the class name to make that relationship clear.
Write the shared field through one reference and read it through another.
class HelloWorld {
// Static Instance variable declaration
private static String str;
public static void main(String[] args) {
// Declaration of a variable 'obj1' of type 'HelloWorld'
HelloWorld obj1 = new HelloWorld();
// Initialization of an instance variable 'str' through local variable 'obj1'
obj1.str = "Hello";
// Print the value of an instance variable 'str' through local variable 'obj1'
System.out.println(obj1.str);
// Declaration of a variable 'obj2' of type 'HelloWorld'
HelloWorld obj2 = new HelloWorld();
// Print the value of an instance variable 'str' through local variable 'obj2'
// It will print the value which as assigned by an object 'obj1'
System.out.println(obj2.str);
}
}
Hello Hello
Reference: Java language reference