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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:27:53+00:00 2026-06-10T18:27:53+00:00

I am developing a library and I don’t want the user to create an

  • 0

I am developing a library and I don’t want the user to create an instance of a class directly with the new keyword, instead I have special methods to create objects. So is it possible if the user instantiates a class using the new keyword, that the compiler gives error.
For Example:

public class MyClass {
    private MyClass(int i) {  }

    public MyClass createMyClassInt(int i) {
        return new MyClass(i);
    }

    private MyClass(double d) { }

    public MyClass createMyClassDouble(double d) {
        return new MyClass(d);
    }
}

So when the user tries to instantiate MyClass myClass = new MyClass();, the compiler will give an error.

  • 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-10T18:27:54+00:00Added an answer on June 10, 2026 at 6:27 pm

    Your code is nearly fine – all you need is to make your create methods static, and give them a return type:

    public static MyClass createMyClassInt(int i) {
      return new MyClass(i);
    }
    
    public static MyClass createMyClassDouble(double d) {
      return new MyClass(d);
    }
    

    The methods have to be static so that the caller doesn’t already have to have a reference to an instance in order to create a new instance 🙂

    EDIT: As noted in comments, it’s probably also worth making the class final.

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

Sidebar

Related Questions

I'm developing for the AVR platform and I have a question. I don't want
We are developing applications for use within AutoCAD. Basically we create a Class Library
I developing on the Linux platform. I want to create a new proccess in
I'm developing a library to IOS, which at some point need to create a
I am developing a library. I want some functionality to be additionally available in
I am developing a class library (C#) that i will use it for my
I'm developing a Class Library / API and I need to store some global
I'm developing a class library and I need to provide a way to set
So I'm developing a new component that requires a little storage requirement. I don't
I'm developing a library in which I have two groups of classes: A) a

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.