Java convention on reference to methods and variables

前端 未结 6 1091
一个人的身影
一个人的身影 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 16:59

    If you use object for static variable access then compiler will replace it with Class Name only.

    So

    MyClass Obj1 = new MyClass();    
    Obj1.variable1;
    Obj1.methodName1();
    

    It is same as

    MyClass.variable1;
    MyClass.methodName1();
    

    Now Why to differentiate? Answer is - It is for better reading If someone see method being called on Class then he immediately come to know that it is static method. Also it prevents generation of one additional object to access the method.

    0 讨论(0)
  • 2021-01-18 17:03

    This has to do with public static methods and variables. Since these methods/variables are associated with the respective class rather than an instance of the class, it is nice to use refer to these methods or variables as className.methodName() or className.variableName

    "Understanding Instance and Class Members" would be a good starting point to learn about the use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class

    0 讨论(0)
  • 2021-01-18 17:11

    It is only because, public static method or public static variable is not associated with any object, but the class. Though the language designer has given the flexibility of invoking them on objects, reader of the code would be confused whether those are static variable/methods or instance methods/variables. So readability is the reason behind asking the developers to invoke them on classes.

    0 讨论(0)
  • 2021-01-18 17:13

    You are allowed to access static members either by using the class name notation or by accessing using an object. It is not recommended to use the object notation since it can be very confusing.

    public class TheClass {
        public static final staticValue = 10;
        public static void staticMethod() {
            System.out.println("Hello from static method");
        }
    
        public static void main(String ... args) {
            TheClass obj = null;
    
            // This is valid
            System.out.println(obj.staticValue);
            // And this too
            System.out.println(obj.staticMethod());
    
            // And this is also valid
            System.out.println(((TheClass)null).staticValue);
            // And this too
            System.out.println(((TheClass)null).staticMethod());
    
        }
    }
    

    It is much clearer if the static methods and variables are called with the class name notation.

    0 讨论(0)
  • 2021-01-18 17:13

    static variable belongs to the class and not to object(instance). A static variable can be accessed directly by the class name and doesn’t need any object. it saves space not having to have variables for the same data for each class.

    Syntax : <class-name>.<variable-name>
    
    public class AA{
    
     static int a =10;
    
    
    }
    

    You can call

    System.out.println(AA.a);
    System.out.println(aObject.a);
    

    There is no differen between two calling but maintain coding convention to keep more readbale

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题