Java convention on reference to methods and variables

前端 未结 6 1094
一个人的身影
一个人的身影 2021-01-18 16:19

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.met

6条回答
  •  醉话见心
    2021-01-18 17:22

    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.

提交回复
热议问题