Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Java object assignment 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 run 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 #4 is not : " Cat : static -- 4 " ? My understanding would be like this :

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

But that doesn't seem to be the case, why ?

like image 966
Frank Avatar asked Apr 28 '15 20:04

Frank


People also ask

What do you mean by object in Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.

What is assigning in Java?

An assignment statement designates a value for a variable. An assignment statement can be used as an expression in Java. After a variable is declared, you can assign a value to it by using an assignment statement. In Java, the equal sign = is used as the assignment operator.

What happens when assign an object to another object in Java?

what happens when you assign one object to another object ?? It assigns the reference of the Object. In other words, both variables 'point' to the same Object. For example/ int [] a = new int[5]; int [] b = a; b and a now 'point' to the same array.


2 Answers

You declare myAnimal as Animal. Therefore a static method is called from that class too.

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

like image 193
Bubletan Avatar answered Sep 28 '22 09:09

Bubletan


From Oracle docs:

8.4.8.2. Hiding (by Class Methods)

If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible to code in C.

Example 8.4.8.2-1. Invocation of Hidden Class Methods

A class (static) method that is hidden can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The 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());
            }
        }

produces the output:

Goodnight, Dick

because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.

like image 38
Alexey Semenyuk Avatar answered Sep 28 '22 07:09

Alexey Semenyuk