Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

When (in Java) a static method and an instance method with the same name exist in different classes, I cannot access the method

First, please take a look at the following code.

package test;

class c_hi {
    public static void method_hi(){
        System.out.println("hi");
    }
}

class c_bye {
    public void method_hi(){
        System.out.println("bye");
    }
}

public class test {
    public static void main(String[] args){
        c_hi.method_hi();
        c_bye c_hi = new c_bye();
        c_hi.method_hi();
    }
}

I've been using Java for several years, and I understand the general rules for naming class names and variable names.

However, I got a very interesting question. If the name of the reference variable of the "c_bye" class is "c_hi" (a class named "c_hi" already exists),

I can't access "method_hi" of class "c_hi" from inside class "test".

Of course, I know that this problem can be prevented or circumvented by not overlapping class names and variable names, package separation, and FQCN etc.

Apart from the usual way of avoiding duplicate names, is there a more grammatical way to solve this problem? Please tell me your opinion. (Or, I would appreciate any documentation, links, or other questions on Stack Overflow that I can refer to.)

This code works the same for both JDK versions 8 and 15.

question from:https://stackoverflow.com/questions/66055886/when-in-java-a-static-method-and-an-instance-method-with-the-same-name-exist-i

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

1 Reply

0 votes
by (71.8m points)

This should "fix" the problem:

    test.c_hi.method_hi();  // Using the fully qualified class name.

However, the correct solution would be:

  1. Don't ignore Java style rules. The rules say Java method name should start with a lowercase letter, and a class name should start with an uppercase letter.

  2. Avoid using the same name for a static method and an instance method.

    (You actually can't do this in some cases. For example, if c_bye extends c_hi then you get a compilation error about an instance method not being allowed to override a static method.)

  3. Don't attempt to call a static method using an instance variable. Use the class name. (Which cannot be confused with a variable name if you follow the style rules!)

    It is legal Java to do that, but it tends to fool the reader into thinking that the method is an instance method and/or that there is dynamic dispatching of static methods happening.

For what it is worth the rules for name resolution are fully specified in the JLS. (See this answer for the JLS text and reference.) The implications are a bit complicated for edge cases like the one you are talking about, but the name resolution rules are not Java version specific, AFAIK.

Java is not designed to "play nice" when people willfully ignore the style rules.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...