Java Static and Final Keyword
Java Static and Final Keyword
Static Keyword
Variables
Static Methods
Block
Class
Syntax
System.out.println(x);
x = 10;
System.out.println(x);
• Variables declared as ‘static’ can be accessed without the use of obje
obj1.x = 18;
obj2.x = 22;
System.out.println(obj1.x);
System.out.println(obj2.x); The value of ‘static’ variable is shared among multiple objects
x = 0;
System.out.println(obj1.x);
System.out.println(obj2.x);
output
} 12
} 10
22
22
0
0
Java Static Methods
Syntax
System.out.println(x);
x = 10;
System.out.println(x);
}
}
public class staticMethodDemo1 { • Static method can be called from static and non-static methods
void nonStaticMethod() {
Non-static method
System.out.println("non-static method called");
show();
}
show();
Static method
staticMethodDemo1 obj1 = new
staticMethodDemo1();
obj1.nonStaticMethod();
} output
} static method called
non-static method called
static method called
Java Static Block
Syntax
static {
}
public class staticBlock1 {
static int x;
static String s; Static variables
static {
System.out.println("static block executed");
x = 1; Static block
s = "static block";
}
System.out.println("Value of x : "+x);
System.out.println("Value of String : "+s);
}
• Static block is executed when the class is loaded to the memory
}
output
static block executed
Value of x : 1
Value of String : static block
public class staticMultipleBlocks1 { • Multiple static blocks
• Observe the order of execution of static blocks
static int x; • Static blocks are executed when the class is
static String s; loaded to the memory
• Observe the value displayed in the ‘main’ method
static {
System.out.println("Block1");
x = 1; Block 1
s = "Block1";
}
static {
System.out.println("Block2");
x = 2; Block 2
s = "Block2";
}
} output
Block1
Block2
value of x: 2
value of String :Block2
public class staticMultipleBlocks1 {
static int x;
static String s;
static {
System.out.println("Block1");
x = 1;
s = "Block1";
}
public staticMultipleBlocks1() {
System.out.println("Constructor called"); What is happening here ?
x = 2;
s = "constructor";
}
static {
System.out.println("Block2");
x = 3;
s = "Block2";
}
System.out.println("value of x: "+x);
output
Block1
System.out.println("value of String :"+ s); Block2
Constructor called
} value of x: 2
} value of String :constructor
Programming Assignment 6 Question for Next week:
‘Final’ keyword is used in java when variable, method, and class definitions shouldn’t change
Variables
Methods
Final
Class
Final Variable
Make observation from the output Make observation from the output
Blank Final Variable
• Final variable which is not initialized at the time of declaration is known as blank final variable
• Blank final variable must be initialized in the constructor, else it will throw error
Make observation from the output Make observation from the output
public class blankFinalVariable1 {
final int x;
static {
x = 100;
}
}
Can the final variable be initialized in a static block ?
If not then how this can happen
public class blankFinalVariable1 {
static {
x = 100;
}
Final class