Movatterモバイル変換


[0]ホーム

URL:


Open In App

Whenever we use astatic keyword and associate it to a block, then that block is referred to as a static block. Java supports static block (also called static clause) that can be used for static initialization of a class. This code inside the static block is executed only once: the first time the class is loaded into memory. 

Calling of static block in Java?

In order to call any static block, there is no specified way, as a static block executes automatically when the class is loaded in memory.

Illustration:

Java
classGFG{// Constructor of this classGFG(){}// Method of this classpublicstaticvoidprint(){}static{}publicstaticvoidmain(String[]args){// Calling of method inside main()GFGgeeks=newGFG();// Calling of constructor inside main()newGFG();// Calling of static block// Nothing to do here as it is called// automatically as class is loaded in memory}}

Note: From the above illustration, we can perceive that static blocks are automatically called as soon as the class is loaded in memory, and there is nothing to do as we have to in case of calling methods and constructors inside main(). 

Can we print something on the console without creating a main() method?

It is a very important question from the interview's perceptive point. The answer is yes we can print if we are using JDK version 1.6 or previous and if after that  it will throw an. Error. 

Example 1-A:  Running on JDK version 1.6 of the Previous

Java
classGFG{// Static blockstatic{// Print statementSystem.out.print("Static block can be printed without main method");}}

Output:

Static block can be printed without the main method

Example 1-B:Running on JDK version 1.6 and later

Java
classGFG{// Static blockstatic{// Print statementSystem.out.print("Static block can be printed without main method");}}

Output: 

Execution of Static Block

Example 1:

Java
classTest{// Case 1: Static variablestaticinti;// Case 2: non-static variablesintj;// Case 3: Static blockstatic{i=10;System.out.println("static block called ");}// End of static block}// Class 2classGFG{// Main driver methodpublicstaticvoidmain(Stringargs[]){System.out.println(Test.i);}}

Output
static block called 10


Remember: Static blocks can also be executed before constructors.

Example 2:

Java
classTest{// Case 1: Static variablestaticinti;// Case 2: Non-static variableintj;// Case 3: Static blocksstatic{i=10;System.out.println("static block called ");}// Constructor callingTest(){System.out.println("Constructor called");}}classGFG{// Main driver methodpublicstaticvoidmain(Stringargs[]){// Although we have two objects, static block is executed only once.Testt1=newTest();Testt2=newTest();}}

Output
static block called Constructor calledConstructor called

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

Note:We use anInitializer Block in Java if we want to execute a fragment of code for every object, which is seen widely in enterprising industries in development. 


Improve
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp