What is the standard for naming an interface in java api

One of my colleagues asks me about the Throwable class in the java API.

According to the standard, I understand that every word ending with * is possibly an interface in the java API. There is an industry standard for using words such as interface names. Therefore, I, unconsciously, told him about this as the basic interface for all exceptions and types of errors in the java world. Then it shows me a java file for this class.

My questions:

  • Why java people chose this name as a class. I think it was supposed to be the default interface?

  • Is this a template for using * skillful words as an interface?

  • Is there another class example ending in * maybe?

Sincerely.

+7
java java-api
source share
5 answers

Very often, for these β€œ-able” names, they are interfaces in Java, but there is no formal interface naming convention that I have found, which suggests that β€œ-able” names should be interface names, although this is usually the case.

The official Java naming conventions can be found here - this is pretty meager, in fact there are no restrictions for naming classes or interfaces:

Regarding your Throwable question, James Gosling once answered why this is a class and not an interface, although the name was more appropriate for the interface.

Unfortunately, the original article from the Sun / Oracle website disappeared on the Internet, so I can only indicate indirect attribution:

edit: As I continue to receive answers to this question, I found a link to the Sun discussion through the Wayback Machine , here: http://web.archive.org/web/20071013225816/http://java.sun.com/features/2002 /03/gosling.html?source=jdc_news&date=20020430

JDC: Why is Throwable not an interface? The type name suggests what it should have been. The ability to catch types, that is, something like try {} catch (), and not just classes. This would make the Java programming language more flexible.

JG: The reason Throwable and the rest of these guys are not interfaces is because we decided, or I decided quite early. I decided that I want to have some kind of state associated with every exception that is thrown. And you cannot do this with interfaces; you can only do this with classes. A condition that is mostly standard. There is a message, there is a snapshot, all that is always there. and also, if you make a Throwable interface, it is tempting to assign any old object to be a Throwable thing. Stewartingly, throwing shared objects is probably a bad idea that the things you want to throw should really be things that are called to be exceptions that really capture the nature of the exception and what happens. These are not just general data structures.

+20
source share

Nouns are always used to refer to classes, but Throwable is an exception.

(See what I did there?)

+29
source share

There are others, such as

And, of course, there are many interfaces that don't end in -able. Some people like to prefix all of their interface names with "I" (IAdjustable instead of Adjustable). Like codes that format wars, theirs is not a universal convention. Sun has a few suggestions , but they are pretty vague.

+2
source share

* Note. Interface names should usually be adjectives, while class names should usually be nouns.

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

There is also some debate as to whether good style is adding the letter i to interfaces. (Example: iCat, IDog, etc.), But this usually applies to other languages, not Java per word.

In my opinion, convention is just a β€œrule of thumb", if convention interferes with the readability of your code, go to a more descriptive interface / class name and not the next convention, but if you are really trying to find a good descriptive class name, maybe you need think a little more about the functionality of your class / interface.

+2
source share

We are talking about naming conventions here and yes, * capability is the preferred convention for naming interfaces. As I'm sure you saw, there are always exceptions. For example, System.arraycopy does not have a camel.

0
source share

All Articles