Suns Java style rules specify that accessors should follow the getX/setX pattern, and most Java API mathods follow this...
except for some very common exceptions such as:
String length()
enum name() [NB new to 1.5, not a legacy!]
ArrayList size()
etc
It seems that there is another pattern operating here: methods that access a private field which will never be set from outside the class are simply named after the field.
Has anyone seen this documented? Is there some other explanation?

Recommended Answers

All 8 Replies

Very interesting. Thanks JC.

I think it is undocumented. I never read about the design pattern of method from a textbook. Presumption !!!. I am pasting a link http://www.javacamp.org/designPattern/ may be some of you visited.

Hi adatapost. Sorry if I caused any confusion by using the word "pattern", I should have said "naming convention". Curious, isn't it?

Hi adatapost. Sorry if I caused any confusion by using the word "pattern", I should have said "naming convention". Curious, isn't it?

Yes, it is. What about the name of static constructor? :)

What's a static constructor? If you mean static initializer, they (by definition) do not have a name, they just appear within the class enclosed in {...}

Which method will be invoked by JVM?

public class Sample{
    static {
           System.out.println("World");
     }
    public static void main(String []args) {
           System.out.println("Hello");
    }
}

"World" will be executed when the class is loaded. After that the main(...) method may (or may not) be executed (depending on whether this is the main class of the application)

It seems that there is another pattern operating here: methods that access a private field which will never be set from outside the class are simply named after the field.
Has anyone seen this documented? Is there some other explanation?

Well, the JLS makes the following recommendation on method names

6.8.3 Method Names
Method names should be verbs or verb phrases, in mixed case, with the first letter lowercase and the first letter of any subsequent words capitalized. Here are some additional specific conventions for method names:

* Methods to get and set an attribute that might be thought of as a variable V should be named getV and setV. An example is the methods getPriority and setPriority of class Thread.
* A method that returns the length of something should be named length, as in class String.
* A method that tests a boolean condition V about an object should be named isV. An example is the method isInterrupted of class Thread.
* A method that converts its object to a particular format F should be named toF. Examples are the method toString of class Object and the methods toLocaleString and toGMTString of class java.util.Date.

Whenever possible and appropriate, basing the names of methods in a new class on names in an existing class that is similar, especially a class from the Java Application Programming Interface classes, will make it easier to use.

That does explain length() and size(), but I'm not sure of their rationale with the name() method (and ordinal() as well).

Thanks for that Ezzaral. Interestingly, the 1999 Sun Java standards are exactly the same, except that the special case for length() is missing. I guess they retrofitted the standards to the code. The enum methods remain a mystery!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.