4

In my lecture notes regarding Static binding, it gives an example using an instance method.

Example: class Animal has method:

void dumb() {int x = 0;}

Then

Animal doudi = new Animal(); 
doudi.dumb();

the compiler can directly insert the body {int x = 0;}

However, from what I understand, this isn't a static method, so why is it using static binding as opposed to dynamic binding?

Also, on this website, it says that instance methods are resolved using dynamic binding.

Is the example here not an instance method?

3
  • 4
    You seem to have just assumed that doudi.dumb(); is resolved with static binding. What made you think that? Commented Jul 10, 2020 at 2:01
  • This other answer contains examples that may extend your investigation of this topic. Commented Jul 10, 2020 at 12:37
  • @Sweeper It is written in my lecture notes that it is resolved using static binding, which is why I am confused. Commented Jul 10, 2020 at 16:25

2 Answers 2

4

To answer question from title, YES, dynamic binding isn't always applied to instance methods.

There are 3 cases where dynamic binding will not happen when method is called, and that is when method is either:

  • final - because it can't be overridden in subtype, so compiler is sure which body of method will need to be executed at runtime
  • private - because private methods are not inherited we can't override them. Subtypes can redeclare their own method with same name and parameter types (signature) but that will not be considered as overriding (you can't add @Override annotation to such method in subtype without compilation error; you can change its return type, for instance from int to void)
  • static - because it is called on class, not a instance (but that is not the subject of the question. In short, even when you write someAnimal.staticMethod() it will be seen by compiler as if you would write Animal.staticMethod())

... so why is it using static binding as opposed to dynamic binding

It ISN'T. In your case when you call doudi.dumb(); compiler sees that dumb() is not final, private nor static so it will be subject for polymorphism (so dynamic binding).

Sign up to request clarification or add additional context in comments.

Comments

1

This is an instance method !!! For an method or attribute of class you need explicit use static modifier. You are creating an instance of Animal and calling the method dump() on this instance. See my example:

public class Animal {
    
    static int x = 0; // is an attribute of class
    int y = 0; // is an instance attribute
    
    
    static void myStaticMethod() { // is an method of class
        x++; // increment the class attribute
//      y++; wrong, compilation error !!! y is an instance attribute
    }
    
    void myMethod() { // is an instance method
        y++; // increment the instance attribute
        x++; // increment the class attribute
    }

}

Another thing about this is, static attributes/methods are assigned to memory on compilation time, while non-static attributes/methods are assigned to memory on execution time. The life-cycle of static attribute/method starts on program execution and ends at end of execution, instance attribute/method lasts (in Java) while reference count for this instance if above then 0, on reference count is 0 the garbage collector frees the allocated memory for this instance. Look:

Animal cat = new Animal();

Animal.myStaticMethod();
cat.myMethod();
        
Animal dog = new Animal();

Animal.myStaticMethod();
dog.myMethod();
dog.myMethod();

System.out.println("Animal::x = " + Animal.x);
System.out.println("cat::y = " + cat.y);
System.out.println("dog::y = " + dog.y);

Output is (without y++ on myStaticMethod):

Animal::x = 5
cat::y = 1
dog::y = 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.