Solved

How to prevent a Non-abstract class from instantiation?

Posted on 2000-05-15
13
287 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now