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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:03:09+00:00 2026-05-13T10:03:09+00:00

class test { test() { System.out.println("Constructor"); } { System.out.println("Hai"); } } public class sample

  • 0
class test
{
    test() {
        System.out.println("Constructor");
    }

    { System.out.println("Hai"); }
}


public class sample
{
    public static void main(String [] a) {
        test t = new test();        
    }
}

In the above code, why is "Hai" printed before the test() constructor is called?

The test() constructor in the test class is above the "Hai" statement and should be called first, right?

  • 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-13T10:03:09+00:00Added an answer on May 13, 2026 at 10:03 am

    Let express with a more clear example:

    public class Test {
    
        static {
             System.out.println("static initializer");
        }
    
        {
             System.out.println("instance initializer");
        }
    
        public Test() {
             System.out.println("constructor");
        }
    
    }
    

    and test it as follows:

    public class Main {
    
        public static void main(String[] args) {
            Test test1 = new Test();
            Test test2 = new Test();
        }
    
    }
    

    output:

    static initializer
    instance initializer
    constructor
    instance initializer
    constructor
    

    The static initializers are executed only once during runtime, specifically during loading of the class. The instance initializers are executed during every instantiation before the constructor.

    You can have more than one of them and they will be executed in the order as they appear in the coding.

    The major benefit of instance initializers is that they are executed regardless of which constructor you use. They applies on each of them so that you don’t need to duplicate common initialization over all of them.

    The major benefit of static initializers is that they are executed only once during class loading. A well known real world example is the JDBC driver. When you do

     Class.forName("com.example.jdbc.Driver");
    

    which only executes the static initializers, then any (decent) JDBC driver will register itself in the DriverManager as follows

     static {
          DriverManager.registerDriver(new com.example.jdbc.Driver());
     }
    

    this way the DriverManager can find the right JDBC driver during getConnection().

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

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't. This would be a security hole. Now that… May 14, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer When a partial submit is executed, the full JSF lifecycle… May 14, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer I suppose you do not want to modify the original… May 14, 2026 at 8:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.