Solved

How to prevent a Non-abstract class from instantiation?

Posted on 2000-05-15
13
312 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
reverse digits of a number using for loop 5 52
Java 8 to Java 6 8 20
jsp error 6 25
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

839 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