Do not wait to strike till the iron is hot; but make it hot by striking....
deepak carter wrote:How can i force that object of a class cannot be created.Can i do it by declaring constructor of that class PRIVATE.
and what will be the use of this kind of class.
Manoj Kumar Jain wrote:You can mark the class as abstract..
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
deepak carter wrote:now i want that no one can create the object of this class.
how can i do this?
deepak carter wrote:Suppose i write this:
class A
{
/* some code here*/
}
now i want that no one can create the object of this class.
how can i do this?
luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie wrote:Wouldn’t you throw an UnsupportedOperationException there?
Campbell RitchieIf you had a checked Exception that would be obvious from the documentation because it would display the[tt wrote: throws [/tt]clause.
Jeff Verdegan wrote:
Campbell Ritchie wrote:Wouldn’t you throw an UnsupportedOperationException there?
Personally, I'd throw an AssertionError, as that is code that should never be executed. As in, "It shouldn't be possible to get here."
Dennis Deems wrote:
Jeff Verdegan wrote:
Campbell Ritchie wrote:Wouldn’t you throw an UnsupportedOperationException there?
Personally, I'd throw an AssertionError, as that is code that should never be executed. As in, "It shouldn't be possible to get here."
But the AssertionError would only occur if we were running with assertions enabled.
I think unsupported operation is a better communication of the designer's intent.
Jeff Verdegan wrote:
Dennis Deems wrote:
Jeff Verdegan wrote:
Campbell Ritchie wrote:Wouldn’t you throw an UnsupportedOperationException there?
Personally, I'd throw an AssertionError, as that is code that should never be executed. As in, "It shouldn't be possible to get here."
But the AssertionError would only occur if we were running with assertions enabled.
Nope. It's the assert keyword that gets turned on and off. If it's on and the assertion fails, then AssertionError is thrown, but AssertionError is just a plain old unchecked exception.
I think unsupported operation is a better communication of the designer's intent.
I think AssertionError is, because it says, "It's supposed to be impossible to get to this code." It's a fine point though, and either one will do the job quite nicely.
Jeff Verdegan wrote:Honestly, I wouldn't even do anything beyond making the c'tor private. It's easy to get carried away with defensive programming.
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|