Java convention on reference to methods and variables


Section 10.2 of Java conventions recommends using class names instead of objects to use static variables or methods, i.e. MyClass.variable1 or MyClass.methodName1() instead of

MyClass Obj1 = new MyClass();

There is no explanation of the rationale behind this, although I suspect this has something to do with memory use. It would be great if someone could explain this.

I guess you mean "for static methods and variables".

There is no difference regarding memory, except of course if you create the instance just for calling the method. Conventions aren't for memory efficiency but for coder efficiency, which is directly related with the readability of the code.

The rationale is that by reading


you know it's a static method and that it can't use or change your Obj1 instance.

And if you write

obj1.variable1; // note the "o" instead of "O", please do follow conventions

then the reader has to read your source code to know if variable1 is static or not.