Since static things loaded at time of class loading,and can be used even before object creation as ClassName.member
but if static method is private,so you can only do this thing ClassName.method()
inside class which is also accessible by directly method()
without appending classname.
Hence making a private
method static
has no significance untill it is not being used in any static
method of same class.since only private can not be use in some other static method of same class.
But I seen some method those are not being use in any other static stuff and still they are static
.For Example hugeCapacity
method of ArrayList
-
private static int hugeCapacity(int minCapacity) {...}
why we not not keep it private only ?
private int hugeCapacity(int minCapacity) {...}
Can one let me know significance behind, making this method static in our java Libraries?
Making this distinction (that is not mandatory) conveys a meaning for clients of the class and gives hints on their behavior.
supposes that the method behavior depends on the current instance. So the client class expects that it may manipulate instance members and optionally static members.
while this
supposes that the method behavior is a class method that is not related to a specific instance. So it can manipulate only static members.
It is not a great optimization but it spares an additional parameter that is passed to each method by the JVM.
You don't see it in the source code but for instance methods,
this
is indeed added in the compiled code as parameter.If you have a method which doesn't use the instance, it is clearer and more efficient to make it
static
.Using a instance method which doesn't actually use
this
can be confusing, and it is potentially more work for the JVM to pass around a reference you don't use.