Reference Static Methods/Variables in Java from an Instance

后端 未结 5 1148
傲寒
傲寒 2020-12-11 20:17

Can anyone explain to me why java allows you to access static methods and members from an instance? A bad example, if I have a class called RedShape and it has a static meth

相关标签:
5条回答
  • 2020-12-11 20:41

    I don't see anything wrong with calling a static method from an instance. What's wrong with that? In particular, quite often there are methods which are useful within the logic of a class, but which don't actually need to manipulate the instance itself.

    I do object to calling a static method via an instance reference. Classic example:

    Thread thread = new Thread(...);
    thread.sleep(5000); // Doesn't do what it looks like
    

    This comes with a compiler warning in some IDEs - certainly in Eclipse, assuming you turn it on. (Java / Compiler / Errors and Warnings / Code Style / Non-static access to static member.) Personally I consider that a mistake in the design of Java. (It's one of the mistakes that C# managed to avoid copying.)

    0 讨论(0)
  • 2020-12-11 20:43

    The access to static methods allows you to share values between instances of the same class or even get the values without needed to create a class instance.

    There are cases where it's convenient and is no OO language violation.

    0 讨论(0)
  • 2020-12-11 20:46

    I bet it's because the original designers were porting capability from C++, and by the time 20/20 hindsight hit, it was a backwards compatibility issue.

    That, or because when you call a method within a class, even though you don't have to prefix everything with this. the compiler inserts it (or equivalent) including for static methods. If static methods couldn't be called from instances, then tacking this. on the front might be a problem (or would force coders to tack class name on the front of static methods whenever they wanted to use them within an actual instance).

    Anyway, the answers are speculative unless we get one of the early language developers to answer.

    0 讨论(0)
  • 2020-12-11 20:55

    There's really no reason why you can actually do this.

    My only guess was that it would allow you to override static methods, but you can't.

    If you try the following scenario:

    Banana has a static method called 'test' (this prints 'banana') Apple extends Banana and "overrides" the static method called 'test' (this prints 'apple')

    and you do something like this:

    public static void main(String[] args) {
        Apple apple = new Apple();
        Banana banana = new Banana();
        Banana base = new Apple();
    
        apple.test();
        banana.test();
        base.test();
    }
    

    The resulting output is:

    apple
    banana
    banana
    

    So effectively, it's pretty useless.

    0 讨论(0)
  • 2020-12-11 20:58
    public class MyClass {
        public static String myString;
    }
    
    
    public class AnotherClass {
       public void doSomething() {
           doAnotherThing();
       }
       public static doAnotherThing() {
           MyClass.myString = "something";
       }
    

    Here we are accessing static variable from non-static method (indirectly) by calling static method from non static method.

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