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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:20:26+00:00 2026-05-18T08:20:26+00:00

i was asked this question in my interview that how to make a class

  • 0

i was asked this question in my interview that how to make a class that can only allows 3 objects creation.

i suggested to take a static variable and a static function for creation of object and while returning reference of new object just check value in static variable to check count of no. of object already created.

second approach i told him to take 3 static object of same class in that class only and let the user use those object only.

please tell me the best approach to perform above operation.

Thanks

  • 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-18T08:20:26+00:00Added an answer on May 18, 2026 at 8:20 am

    I think your first approach is closer to the question: make private constructor and a static newInstance() method that checks how many instances were created before and returns null if it’s more than 3.

    Second approach is also ok, though.

    EDIT @Saurabh: even though the question says nothing about what to do in case the object is gc-ed, let’s develop this:

    1. A dirty hack to the first solution: override finalize() method that decrements the static counter of objects.
    2. A pool of objects with some sort of a locking mechanism that only allows three, let’s say, users, use the objects ate the same time.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked this question during an interview. They're both O(nlogn) and yet most
I was asked this question in a job interview. The interviewer and I disagreed
I originally asked this question , but in finding an answer, discovered that my
I was asked this question in an interview Plz tell me the answer :-
I was asked this question in an interview for an internship, and the first
I was asked this question in some interview. I was required to write code
I was asked this question in an interview. Although the interview was for dot
I have been asked this question in interview how does XML and Soap work
Hii , I was asked this question in a recent interview for which i
(I asked this question in another way , and got some interesting responses but

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.