What everyone else said about optimization is absolutely true.
In terms of performance, there is no reason for the inline method. If this is a performance issue, the JIT in your JVM will enable it. In java, method calls are so close to free that you should not think about it.
Saying, there is another problem. Namely, it is bad programming practice to call an override method (i.e., one that is not final , static or private ) from the constructor. (Effective Java, 2nd ed., P. 89 in an element called “Design and document for inheritance or prohibition of it”)
What happens if someone adds a subclass of BinarySearchTree called LoggingBinarySearchTree that overrides all public methods with code like:
public void clear(){ this.callLog.addCall("clear"); super.clear(); }
Then LoggingBinarySearchTree will never be constructive! The problem is that this.callLog will be null when the BinarySearchTree constructor works, but the clear called, which is called, is overridden, and you get a NullPointerException .
Note that Java and C ++ are different here: in C ++, the constructor of the superclass, which calls the virtual method, ends up calling what is defined in the superclass, not the redefined one. People switching between two languages sometimes forget about it.
Given this, I think that perhaps in your case it might be cleaner to embed the clear method when calling from the constructor, but in general, you have to go to Java and make all the method calls that you want.
Daniel Martin Jun 27 '11 at 15:37 2011-06-27 15:37
source share