Solved

How to prevent a Non-abstract class from instantiation?

Posted on 2000-05-15
13
321 Views
Last Modified: 2010-04-01
Here I have two twisting questions in Java?
1. How can prevent my non-abstract class from instantiation. Mind that it is strictly a non - abstract class( Not an interface)?

2. I would like to create only three objects for a class . If more than that, It should give an error message. Please tell me the secret behind it.?
0
Comment
Question by:sjhyam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +3
13 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 2810276
1.for ur first question u can use
public final class myClass
{

}
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810288
1. Make all constructors private or if the class doesn't have a constructor provide a private one.

2. make the constructor private, provide a class factory for that object or a static function that keep in a static variable the number of all instances; when the value in that variable will exceed 3 then return null or throw an exception;
the number of instances can be managed from the constructor also.
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2810291
1. How can prevent my non-abstract class from instantiation. Mind that it is strictly a non -  abstract class( Not an interface)?

Answer: Make the constructor private or protected.  Then provide a method named something like: newInstance() that would create an object using the private constructor if needed.

2. I would like to create only three objects for a class . If more than that, It should give an error message. Please tell me the secret behind it.?

Answer: Using the same methodology as in the above example, provide only protected or private constructors.  Again, these will only be available to the class, and therefore you can provide methods that would control creation of objects of the class.  In the newInstance() method you can call the constructor the first three times requested (and keep a count of the number of objects created).  After the three are created, you can return a null handle, or a handle to one of the already created objects (a variation on the Singleton pattern.), or you can throw an exception.

Good luck

Laminamia :)
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2810293
Oops, sorry ovidiucraciun.  

I'll make my answer a comment.

Laminamia :)
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810305
for the first question:
you can put your class in a package and make it visible only for the classes inside the package; in that way when you'll deploy the binary files the user will can instantiate only the public classes of the package.
of course if the user is clever enough he can avoid this kind of protection ;)
0
 
LVL 2

Author Comment

by:sjhyam
ID: 2810391
This question has a deletion request Pending
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810460
This question no longer is pending deletion
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2810461
hmmm ... ;)
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2811425
sjhyam:

If someone has given a comment that you feel deserves the points because it answers the question, then click on the button "Accept comment as answer" on the comment that answers your question.

Laminamia :)
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2812405
>>>>>Here I have two twisting questions in Java?

some people provide satisfactory answers ,those people don't get any money only satisfaction,so please give those deserving people points.

They both have already answered satisfactorily,pls don't take offence at this.

:)
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2812410
rjackman,
when u want to prevent subclassing  of your class you tag it final, but we can still make insatnces of that class...
0
 

Accepted Solution

by:
ninnu earned 50 total points
ID: 2826939
For your first question
 having a private constructor would be the better way.
for your second question
  you can have a global static variable intialized to zero and keep on increment it when a new object has been constructed and check for it in the constructor.If it is more than 3 ,display the error message.
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2827170
Ninnu.  I believe that the answer was already given in several above comments.

shyjam should accept one of those as the answer.

Would you please withdraw your answer due to the fact that other experts already left the answer in comments?

Laminamia :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question