Java Static Block, Methods and Variables

Static Block

A static block is used for initializing the static variables. This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

package com.javacodestuffs.core.staticin.java; public class StaticBlock1 { static int number; static String str; static { number = 100; str = "static in java"; } public static void main(String args[]) { System.out.println("the value of num: " + number); System.out.println("the value of mystr: " + str); } } output: the value of num: 100 the value of mystr: static in java

Multiple Static blocks

We can have multiple static blocks in java. The blocks get executed as they defined in the sequence. The values initialized by the first block are overwritten by the second block.

package com.javacodestuffs.core.staticin.java; public class StaticBlock2 { static int number; static String str; static { number = 100; str = "Block1"; System.out.println("\ninside static Block 1"); System.out.println("the value of num : " + number); System.out.println("the value of mystr : " + str); } static { number = 200; str = "Block2"; System.out.println("\ninside static Block 2"); System.out.println("the value of num : " + number); System.out.println("the value of mystr : " + str); } public static void main(String args[]) { System.out.println("\ninside main method "); System.out.println("\nthe value of num : " + number); System.out.println("the value of mystr : " + str); } } output: inside static Block 1 the value of num : 100 the value of mystr : Block1 inside static Block 2 the value of num : 200 the value of mystr : Block2 inside main method the value of num : 200 the value of mystr : Block2

Java Static Variables

A static variable is common to all the instances of the class because it is a class-level variable. Only a single copy of the static variable is created and shared among all the instances of the class. Memory allocated for static variables, when the class is loaded in the memory.

1). Static variables are also known as Class Variables.

2). Static variables can be accessed directly in static and non-static methods.

package com.javacodestuffs.core.staticin.java; public class StaticVariables1 { static int number; static String str; static void display() { number = 10; str = "hello world"; System.out.println("inside display method"); } public static void main(String args[]) { display(); System.out.println("value of number is : " + number); System.out.println("value of string is : " + str); } } output: inside display method value of number is : 10 value of string is : hello world

Overriding with respect to static methods

Case 1:

We can't override a static method as non-static

package com.javacodestuffs.core.staticin.java; class Parent { public static void display(){ System.out.println("Parent class display"); } } class Child extends Parent { public void display(){ System.out.println("Child class display"); } } public class Main { public static void main(String args[]) { } } output: com/javacodestuffs/core/statics/Main.java:12: error: display() in Child cannot override display() in Parent public void display(){ ^ overridden method is static 1 error

Case 2:

Similarly, we can't override a non-static method as static.

Case 3:
class Parent { public static void methodOne() {} } class Child extends Parent { public static void methodOne() {} }

It is valid. It seems to be overriding concept is applicable for static methods but it is not overriding it is method hiding.

Post/Questions related to   Java Static Block, Methods and Variables

Can you overload or override static method in Java?
Can you override a private method in Java?
Can you run a Java program without the main() method?
Can you make an array volatile in Java?
Can you overload or override the main method in Java?
Can an Abstract class have a constructor in Java?
Create a Static class in Java

In this article, we have seen the  Java Static Block, Methods, and Variables with examples. All source code in the article can be found in the GitHub repository.