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 8563963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:05:25+00:00 2026-06-11T17:05:25+00:00

here is the code public class ClassResolution { static class Parent { public static

  • 0

here is the code

public class ClassResolution {
static class Parent {
    public static String name;
    static {
        System.out.println("this is Parent");
        name = "Parent";
    }

}

static class Child extends Parent {
    static {
        System.out.println("this is Child");
        name = "Child";
    }

}

public static void main(String[] args) throws ClassNotFoundException {
    System.out.println(Child.name);
}}

what ouput i expect is:

this is Parent
this is Child
Child

but actually is:

this is Parent
Parent

it seems static block in Child class does not get executed, but why? it’s anti-intuition, doesn’t it?

supplement:
to make it more clear, i list the 2 1 points below:

  1. As @axtavt say, according to JLS 12.4.1, class Child is loaded, but not initialized.
  2. But @Alexei Kaigorodov pointed out, according to jvms-5.5,
    class Child should be initialized, because of the execution of
    instruction getstatic on Child class.

what do you think?

supplement2:
@Alexei Kaigorodov has renewed his mind, so it seems no disagreement left. But I think the point of Alexei Kaigorodov is enlightening, so I left it there.

Thank you, everyone.

  • 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-11T17:05:26+00:00Added an answer on June 11, 2026 at 5:05 pm

    From JLS 12.4.1:

    A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

    • T is a class and an instance of T is created.
    • T is a class and a static method declared by T is invoked.
    • A static field declared by T is assigned.
    • A static field declared by T is used and the field is not a constant variable (§4.12.4).
    • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

    As you can see, nothing of these happens in your code (note that name is declared in Parent, not in Child), therefore Child doesn’t get initialized and its static block doesn’t get executed.

    If you do something to trigger initialization of Child, you’ll get an expected output:

    new Child();
    System.out.println(Child.name); 
    

    Note, however, that static fields are not inherited, therefore Child.name and Parent.name actually refer to the same field. That’s why it doesn’t make much sense to use code similar to your example in practice.

    Also note that despite the fact that Child.name actually refers to Parent.name, it’s still referenced as Child.name in the bytecode, therefore your code triggers loading of Child, but not its initialization.

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

Sidebar

Related Questions

here is my code public class SimpleJoglApp extends JFrame { public static void main(String[]
In the following code: public class A { public A():this(null){} public A(string b){/*code here*/}
I've been working on this assignemnt here's code: public class Student { private String
Here is my code: public class Main { public static void main(String[] args) {
Here is my code: public class Test { static { main(null); } public static
So here's my code : public class SetWritable<T extends Writable> implements Writable { private
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient
Here is my code: public partial class Image : System.Web.UI.Page { protected void Page_Load(object
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here's my code: public class MyActivity extends Activity implements View.OnClickListener { private Button mHorizontalButton;

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.