Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does OpenJDK place private methods into vtable?

It seems that openJDK 8 places private methods which are not final nor static into vtable. Why is it so when dynamic binding is not used for private methods (since they're invoked with invokespecial) or is it used?

like image 504
Radek Micek Avatar asked Feb 15 '15 16:02

Radek Micek


People also ask

Why would you make a method private?

Private methods are useful for breaking tasks up into smaller parts, or for preventing duplication of code which is needed often by other methods in a class, but should not be called outside of that class.

Are instance methods private or public?

Public instance methods: - All instance methods are public by default. - Use if displaying information or interacting with other classes and/or the client. Private instance methods: - Accessible only from within class scope.

What is a private instance method?

As of Java 9, methods in an interface can be private. A private method can be static or an instance method. Since private methods can only be used in the methods of the interface itself, their use is limited to being helper methods for the other methods of the interface.


1 Answers

This is done to handle some rare situations when an overridable method with the same name and signature exists in a superclass. Though there is definitely a place for improvement, may be, targeted for JDK 9.

See https://bugs.openjdk.java.net/browse/JDK-8024368

Private methods always get a vtable entry to handle backward compatibility with classes - i.e. you can have the same name of a private method local to your class and also inherit a method of from your superclass, which will get inherited around the private method, by your child.

like image 187
apangin Avatar answered Sep 29 '22 12:09

apangin