Solved

How to prevent a Non-abstract class from instantiation?

Posted on 2000-05-15
13
316 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
Technology Partners: 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!

 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 36
use lov values 2 71
batch vs regular insert in spring DAO 2 20
Java array 21 79
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

735 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