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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:54:48+00:00 2026-05-12T09:54:48+00:00

Recently I’ve faced a question : How to avoid instantiating a Java class? However,

  • 0

Recently I’ve faced a question : How to avoid instantiating a Java class?

However, I answered by saying:

  1. If you don’t want to instantiate a class, use “abstract” modifier. Ex: javax.servlet.HttpServlet, is declared as abstract(though none of its methods are abstract) to avoid instantiation.

  2. Declare a no argument private constructor.

Now my question is
a) are there any other ways?
b) why does any one do not want to instantiate a class? – after searching in SO, I got to know from this that Util classes can be made not to instantiate. Any other places where we don’t want to instantiate a class in OOP?

  • 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-12T09:54:49+00:00Added an answer on May 12, 2026 at 9:54 am

    Four reasons spring to mind:

    1. To allow subclasses but not the parent to be instantiated;
    2. To disallow direct instantiation and instead provide a factory method to return and if necessary create instances;
    3. Because all the instances are predefined (eg suits in a deck of cards) although since Java 5, typesafe enums are recommended instead; and
    4. The class really isn’t a class. It’s just a holder for static constants and/or methods.

    As an example of (2), you may want to create canonical objects. For example, RGB color combinations. You don’t want to create more than one instance of any RGB combo so you do this:

    public class MyColor {
      private final int red, green, blue;
    
      private MyColor(int red, int green, int blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
      }
    
      public static MyColor getInstance(int red, int green, int blue) {
        // if combo already exists, return it, otherwise create new instance
      }
    }
    

    Note: no no-arg constructor is required because another constructor is explicitly defined.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the end I solved the problem with an ugly… May 12, 2026 at 10:10 pm
  • Editorial Team
    Editorial Team added an answer Would you think this answer is what you are looking… May 12, 2026 at 10:10 pm
  • Editorial Team
    Editorial Team added an answer Why don't you simply change your holder class to template<… May 12, 2026 at 10:10 pm

Related Questions

Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently I had to develop a SharePoint workflow, and I found the experience quite
Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's
Recently I was talking to a co-worker about C++ and lamented that there was
recently I was given the task to discover a C# solution I have never

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.