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

  • Home
  • SEARCH
  • 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 7559101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:32:59+00:00 2026-05-30T12:32:59+00:00

It is my understanding that every time a class instance is created the instance

  • 0

It is my understanding that every time a class instance is created the instance init block runs. When I tried to test this, the instance init block ran for the first two created instances, but not the third.

Here’s the code:

class ModuleInit {
    ModuleInit (int x) { System.out.println("1-arg const"); }
    ModuleInit () { System.out.println("No-arg const"); }
    static { System.out.println("First static init"); }
    { System.out.println("1st instance init"); }
    { System.out.println("2nd instance init"); }
    static { System.out.println("2nd static init"); }

    public static void main(String[] args)
    {
        new ModuleInit();
        new ModuleInit(8);
    }
}

and here’s the output:

First static init
2nd static init
1st instance init
2nd instance init
No-arg const
1st instance init
2nd instance init
1-arg const
               <---Missing instance init for ModuleInt(int x)?

I thought that when the new class instance with the int arg was created ( ModuleInit(8) ) that there should be another instance init block run. Can someone explain why this doesn’t happen here?

  • 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-05-30T12:33:01+00:00Added an answer on May 30, 2026 at 12:33 pm

    The instance initialization blocks run as the first line within the constructors (that is, after the call to super() or this(), whether implicit or declared), so they are running before the constructor prints out its line:

    First static init
    2nd static init
    1st instance init
    2nd instance init
    No-arg const
    1st instance init  <---- They are right here...
    2nd instance init  <---- They are right here...
    1-arg const
    

    This code would be equivalent:

    class Module4 {
        Module4 (int x) { 
            System.out.println("1st instance init");
            System.out.println("2nd instance init");
            System.out.println("1-arg const"); 
        }
        Module4 () { 
            System.out.println("1st instance init");
            System.out.println("2nd instance init");            
            System.out.println("No-arg const"); 
        }
        static { System.out.println("First static init"); }
        //{ System.out.println("1st instance init"); }
        //{ System.out.println("2nd instance init"); }
        static { System.out.println("2nd static init"); }
    
        public static void main(String[] args)
        {
            new ModuleInit();
            new ModuleInit(8);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am I right understanding that def is evaluated every time it gets accessed lazy
It is my understanding that I can test that a method call will occur
It is to my understanding that one should use a forward-class declaration in the
Almost every time I close SQL Server Management Studio I get a prompt that
Understanding that I should probably just dig into the source to come up with
It's my understanding that nulls are not indexable in DB2, so assuming we have
It's my understanding that common wisdom says to only use exceptions for truly exceptional
It is my understanding that the default behavior when creating a table in SQL
It's my understanding that StackOverflow (SO) was built using ASP.NET. What surprised me is
While it's my understanding that there's no fundamental reason a program written for 32-bit

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.