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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:36:51+00:00 2026-06-05T05:36:51+00:00

This question might be blunder to some of the Java Experts. But I would

  • 0

This question might be blunder to some of the Java Experts. But I would like to know why we create the objects in a static method like main but not in static block.
I understand that its going to create the object unnecessarily if we instantiate in static block and of course if we don’t use it further. Are there any other things that are to be noted with this approach ? Can we relate this with Singleton Pattern ?

For example:

public class MyClass {

    static {
        AnotherClass object = new AnotherClass();
            // Do Some operations here with object.
    }
}
  • 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-05T05:36:53+00:00Added an answer on June 5, 2026 at 5:36 am

    The main reason is control over when it actually gets executed.
    The stuff in a static block will get executed the first time that the class is loaded and it’s easy to accidentally cause a class to be loaded (for instance by referencing a constant on the class).

    Having a static method means that you have complete control over when the method is called (because you have to explicitly call it).

    In regards to singletons, the java idiom for eagerly loaded singletons initializes the instance as a static field. This will basically run just the same as a static block.

    public class Singleton {
        private static final Singleton instance = new Singleton();
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            return instance;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question might be repeated many times but would really appreciate any
I know that this question might have been asked like 100 times, but, believe
i know this question might look like a duplicate. but i had a hard
I know this question might have been asked a few times already, but I
I know this question might have already been discussed a thousands of times but
This question might seem very specific but I am in need of some ideas
i know this question might be asked before, but i want to make it
I know this question might sound quite stupid, but I've tried my best and
I know this question might seem a little basic stuff but I want to
I know this question might sound a little bit crazy, but I tough that

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.