What does the purpose of Java objects mean?

I have the following 2 classes:

class Animal { public static void staticMethod(int i) { System.out.println("Animal : static -- " + i); } public void instanceMethod(int i) { System.out.println("Animal : instance -- " + i); } } class Cat extends Animal { public static void staticMethod(int i) { System.out.println("Cat : static -- " + i); } public void instanceMethod(int i) { System.out.println("Cat : instance -- " + i); } public static void main(String[] args) { Cat myCat = new Cat(); myCat.staticMethod(1); // Cat : static -- 1 myCat.instanceMethod(2); // Cat : instance -- 2 System.out.println(""); Animal myAnimal = myCat; Animal.staticMethod(3); // Animal : static -- 3 myAnimal.staticMethod(4); // Animal : static -- 4 [ ? ] System.out.println(""); myAnimal.instanceMethod(5); // Cat : instance -- 5 } } 

And when I launch Cat, I got the following results:

 Cat : static -- 1 Cat : instance -- 2 Animal : static -- 3 Animal : static -- 4 Cat : instance -- 5 

I can understand 1,2,3 and 5, but why number 4 is not: "Cat: static - 4"? My understanding would be this:

myAnimal = myCat means that "myAnimal" is now exactly the same as "myCat", so wherever "myAnimal" appears, you can replace it with "myCat" and get the same result, because everything inside myAnimal is the same as and everything is inside myCat, so "myAnimal.staticMethod (4)" should be the same as "myCat.staticMethod (4)" and the output should be: "Cat: static - 4", similar to "myCat.staticMethod (1) ", above.

But it doesn't seem so, why?

+6
source share
5 answers

From Oracle Docs :

8.4.8.2. Hiding (by class method)

If the class C declares or inherits the static method m, then m is called to hide any method m ', where the signature m is a sub-label (ยง8.4.2) of signature t', in superclasses and superinterfaces C that would otherwise be available for code in C.

Example 8.4.8.2-1. Calling Hidden Class Methods

A hidden class (static) method can be called using a reference whose type is a class that actually contains a method declaration. In this regard, hiding static methods is different from overriding instance methods. Example:

 class Super { static String greeting() { return "Goodnight"; } String name() { return "Richard"; } } class Sub extends Super { static String greeting() { return "Hello"; } String name() { return "Dick"; } } class Test { public static void main(String[] args) { Super s = new Sub(); System.out.println(s.greeting() + ", " + s.name()); } } 

outputs the result:

Good night dick

because the hello call uses the type s, namely Super, to find out at compile time the class method for the call, while the call to the name uses the class s, namely Sub, to find out, at run time, the instance method for the call.

+2
source

You declare myAnimal as Animal . Therefore, a static method is called from this class.

You should never call static methods (or access static fields) from an instance to prevent this confusion.

+3
source

The reason is that Java allows static methods based on the type of the reference variable itself, and not polymorphically at run time, as happens with instance methods.

To expand a bit, when you do Animal myAnimal = myCat , you assign a Cat link to an Animal link. This is acceptable since a Cat also an Animal , so all that Animal can do is Cat .

In addition, if you call the instance method (that is, not static) using the myAnimal reference, and the method is overridden in Cat , then the version of the Cat method is called, because therefore this method was first of all re-evaluated. Static methods, on the other hand, are never exaggerated. That is why they are "static", as in "non-dynamic". This means that static methods can be resolved by the compiler instead of relying on the runtime.

+2
source

Static means exactly that: the call is resolved statically (in your case, it is resolved based on the declared type of the variable, and the variable is an object of compilation time).

The expected result will require that the call be resolved dynamically (polymorphically, based on the actual type of the reference instance, and the instance is the runtime).

+1
source

When you set myAnimal = myCat, the myAnimal pointer points to a cat object, but when you try to access the static method using the myAnimal pointer, it accesses the static method from the class declared by myAnimal.

+1
source

Source: https://habr.com/ru/post/986188/


All Articles