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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:44:51+00:00 2026-06-13T12:44:51+00:00

I know there are lot of questions on Singleton pattern. But here what I

  • 0

I know there are lot of questions on Singleton pattern. But here what I would like to know about the output which might also cover how “static” works in Java.

public class Singleton {
    private static Singleton currentSingleton = new Singleton();

    public static Singleton getSingleton() { 
        return currentSingleton;
    }

    private Singleton() {
        System.out.println("Singleton private constructor...");
    }

    public static void main(String[] args) {
        System.out.println("Main method...");
    }

    }

This is the output from running the code…

Singleton private constructor…
Main method…

When I debugged this code, control went first to line
System.out.println("Singleton private constructor...") and prints.
(private static variable currentSingleton is still null at this point)
Then it goes to line
private static Singleton currentSingleton = new Singleton();
and then initializes the private variable.
Then at last, it goes to main() method and prints.

My Questions are:

  1. Why it first prints “Singleton private constructor…” which is in private constructor.
    I thought control should first go to main() method since it is the entry point. Also I am not creating any instance anywhere (except in variable initialization).
  2. Later it goes to static variable instantiation line (currentSingleton=null at this point)
    private static Singleton currentSingleton = new Singleton();
    Though currentSingleton gets a value here, why constructor is not called again?

Mainly I want to know the flow of control of this program.

  • 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-13T12:44:52+00:00Added an answer on June 13, 2026 at 12:44 pm

    You cannot invoke the main method in the class until it has been properly initialized (i.e. static fields and static blocks have been evaluated). When it is initialized, an instance of your singleton is created by invoking the private constructor. Later the main method is invoked.

    The class in question has a static field to which you assing a value. Since the field is static it must be initialized before the class can be used in any context, that is, it must receive a value. In this case its value happens to be an instance of the same class. This is what triggers your private costructor during class initialization.

    If you want to delve into the process and understand it better please refer to the Java Laguage Specification. More specifically in the section12.4 Initialization of Classes and Interfaces you will find further details.

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

Sidebar

Related Questions

I know there are a lot of questions on here about this, but most
I know there's a lot of questions about this but I've been struggling with
ok, I know there are a lot of questions here about this, that are
I know there are LOT of questions like that but I can't find one
I know there a lot of questions about this, but neither one of them
I know there are a lot of questions on here about email validation and
Ok, so I know that there are a lot of questions like this, but
I know that there has been a lot of questions like this but I
I know there are a lot of questions about aligning Facebook buttons but they
I know there are a lot of questions about it, but I tried several

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.