Since private methods are implicitly final.
private or static or final methods are early bind means they can't be overridden.
But in my code it is actually running properly.
public class B extends A {
public static void main(String[] args) {
new B().privateMethod(); //no error -output B-privateMethod.
}
private void privateMethod() {
System.out.println("B-privateMethod.");
}
}
class A{
private void privateMethod() {
System.out.println("A-privateMethod.");
}
private static void privateStaticMethod() {
System.out.println("privateStaticMethod.");
}
}
Also I want to make sure what the benefit is of making a private member static, besides the fact that you can use class-name.member, over a non-static member within a class.
They are private so can't be used outside the class. For example the hugeCapacity()
method in the ArrayList
class.
private static final int DEFAULT_CAPACITY = 10;
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}