Local Variable
Local variable in java has the privilage to declare variable that are limited in scope to the block.
Java offers a place-holder or a container to hold a value in a memory location till the program code execution gets finished called as Variables with a specified data type and the name of the variable is an identifier for the specified memory location.
Local variable in java has the privilage to declare variable that are limited in scope to the block.
Declare a variable str and initialize it, finally print the assigned value of the local variable str.
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);
}
}
Declare and initialize the variable str in a same statement and finally print the assigned value of a local variable.
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);
}
}
Declare a variable and then initialize the variable str in two different block scopes i.e. outer block scope and inner block scope respectively, finally print the assigned value of a local variable str in both the block scopes.
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);
}
}
Declare a variable and then initialize the variable str in two different block scopes i.e. outer block scope and inner block scope respectively, it will throw an error at the time of code compilation.
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
}
}
Non Static Instance Variables are defined in a main block of a class. It can be accessed only through the object of a class (i.e. each object has its own state). Moreover, these Instance variables have to be prefix with an access modifier based on its access level. Moreover, each class object has its own value for the same instance variable.
Declare a variable and then initialize the non static instance variable str through an object, finally print the assigned value of an instance variable.
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);
}
}
Create two different object for the same class HelloWorld and then initialize the non static instance variable with a different values, finally print the assigned value of a non static instance variable.
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);
}
}
Declare and initialize the Instance variable str in the class level, finally print the assigned value of a non static Instance variable through an object of a class.
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);
}
}
Static Instance Variables are defined in a main block of a class. It can be accessed only through the object of a class (i.e. each object has its own state). These variables should be declared with an access modifier based on its access level. When a variable is declared as static, then a single copy of the memory location is created and shared among all objects at a class level. Even though you are accessing these variables by creating any number of objects, the value of the variable will always remain same.
Create two different objects for the same class HelloWorld and then initialize the static instance variable str using the object obj1. Now print the assigned value of a static instance variable using both of the objects created, the result of the both objects will have the same value.
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);
}
}