Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:52:52+00:00 2026-06-12T23:52:52+00:00

Possible Duplicate: Static Initialization Blocks Unusual static method declaration I’m trying to prepare for

  • 0

Possible Duplicate:
Static Initialization Blocks
Unusual “static” method declaration

I’m trying to prepare for a OCJPC in the near future, and I came across a construct I had never seen before. As it is tough to google for programming constructs, I am asking the question here. The code fragment:

class Geryon {
    static { System.out.print("a"); }
    { System.out.print("b"); }
    Geryon(String s) { System.out.print(s); }

    public static void main(String[] args) {
        new Geryon("c");
    }
}

I am referring to the 2 print statements inside theGeryon() method header in the place where I would expect a return type. As far as I was able to google, a static method header consists of:

access-modifier keyword-"static" return-type|void method-name

Judging from the answer to the quiz question, the code does not only compile, but will also be run. Can anyone direct me to a source where this is explained?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:52:53+00:00Added an answer on June 12, 2026 at 11:52 pm

    A is a static initializer that gets called when the class is initialized by classloader, this :

     static { System.out.print("a"); }
    

    And the other (B) is an anonymous block that gets called every time the class is instantiated :

    { System.out.print("b"); }
    

    Oh and the third print statement, C, is just a normal constructor call.

    You will get all three lines if you instantiated one Geryon, like the code you posted. But then the next time you instantiated a Geryon you will only get two – B and C; as it has already been initialized by classloader, so the static block does not get called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Static Initialization Blocks While reviewing some old code, I found some rather
Possible Duplicate: Static Initialization Blocks Consider the following code : public class Test {
Possible Duplicate: (static initialization/template instantiation) problems with factory pattern trying to force static object
Possible Duplicate: non static method cannot be referenced from static context hey i have
Possible Duplicate: What is the static variable initialization order in C#? For fun i
Possible Duplicate: C# virtual static method Idea: public class Item { public Vector2 Position;
Possible Duplicate: Call non-static method from static method c# We can call non-static method
Possible Duplicate: Java static class initialization Why is the string variable updated in the
Possible Duplicate: C++ Static member method call on class instance Today I discovered that
Possible Duplicate: Static Block in Java I came across the following use of static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.