tags:

views:

123

answers:

5

Hi,

I am asked by one of colleague about the Throwable class in java API.

As per standard, I do understand, every word ending *able is a interface in java API. There is a industry standard about using such words as Interface names. So, I unknowingly, told him about this as base interface for all the exception and error types in java world. Then he shows me the java file for this class.

My questions:

  1. Why java people has choosen this name to be a class. I think this should have been a interface by default?

  2. Is this a pattern to use *able words as interface?

  3. Is there any other example of class ending with *able?

Regards.

+10  A: 

Nouns are usually used to name classes, but Throwable is an exception.

(See what I did there?)

Daniel Alexiuc
careful with words like "always". *Collection*, *Map*, *Set*, *List*, ... would like a word. But yes, I saw what you did :)
bemace
+1 for the pun =8-)
Yuval
maybe Throwable is an error :D
mrrtnn
Nice one.. :-))
Grodriguez
A: 

We are talking about naming conventions here and yes, *able is the preferred convention for naming interfaces. As I'm sure you've seen, there are always exceptions. For example, System.arraycopy is not camel cased.

Kirk Woll
+2  A: 

There are others such as

And of course there are plenty of interfaces that don't end in -able. Some people like to prefix all their interface names with an 'I' (IAdjustable instead of Adjustable). Like code formatting wars, their isn't universal agreement. Sun has some suggestions but they are pretty vague.

bemace
+6  A: 

It's very common for those '-able' names to be interfaces in Java, but there is no official convention for interface naming that I've found that suggests that '-able' names should be interface names, though typically that is the case.

Official Java naming conventions can be found here - it's pretty lean, there really aren't any restrictions for class or interface naming:

As to your Throwable question, James Gosling once answered why it's a class rather than an interface, even though the name was more fitting for an interface.

Unfortunately, the original article from Sun/Oracle's site has vanished into the internet ether, so I can only provide indirect attribution:

JDC: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for types, that is, something like try{}catch (), instead of only classes. That would make the Java programming language much more flexible.

JG: The reason that the Throwable and the rest of those guys are not interfaces is because we decided, or I decided fairly early on. I decided that I wanted to have some state associated with every exception that gets thrown. And you can't do that with interfaces; you can only do that with classes. The state that's there is basically standard. There's a message, there's a snapshot, stuff like that that's always there. and also, if you make Throwable an interface the temptation is to assign, to make any old object be a Throwable thing. It feels stylistically that throwing general objects is probably a bad idea, that the things you want to throw really ought to be things that are intended to be exceptions that really capture the nature of the exception and what went on. They're not just general data structures.

birryree
+1 great info from JG I hadn't seen before
bemace
+1 for having good watch on internet, Thanks for your answer.
vijay.shad
A: 
  1. Interface names should always be an adjective and if possible should end in "able" has been the trend in naming conventions within the Java community. That does not have to be followed strictly, its just a naming convention, there is nothing to stop you from naming the interface/class whatever you want.

  2. Yes, see http://www.iwombat.com/standards/JavaStyleGuide.html#Class%20and%20Interface%20Names

  3. Clonable

*Note, Interface names should generally be adjectives, whereas class names should generally be nouns.

Check out page 15 of this style guide document released by Sun. http://java.sun.com/docs/codeconv/CodeConventions.pdf

There is also some debate as to if it is a good style to add the letter I to interfaces. (Example: ICat, IDog, etc.), but that generally applies to other languages and not to Java per say.

In my personal opinion a convention is just a "rule of thumb", if the convention gets in the way of the readability of your code, go with a more descriptive interface/class name instead of the one that follows convention, but if your really struggling to come up with a good descriptive class name, maybe you need to think about the functionality of your class/interface again a bit more.

Ben Holland
@ben you mean : Cloneable, Others are does not seems to be in Java API.
vijay.shad
@vijay.shad : thanks, my bad, corrected.
Ben Holland