Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

object - 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();    
Obj1.variable1;
Obj1.methodName1();

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

MyClass.methodName1()

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...