0

As for my understanding:

When a method is static it is

  1. early bind
  2. can call with the name of class even before no object is created
  3. can call only static member inside it.

I never found any other behaviour of static either at compile time or runtime. Is there any?

When a method is private it is

  1. also early bind
  2. can only call inside call so can call directly without object.

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;
} 

Since there are in Java private+static method exist. Why their need occurs. Is it for restriction purpose, to restrict access of non static variables inside a method?

Uooo
  • 6,204
  • 8
  • 36
  • 63
TheCurious
  • 593
  • 1
  • 4
  • 29
  • 6
    The point is that static methods are associated with the type rather than with any instance of the type, whereas instance methods are associated with an instance of the type. The private part is entirely orthogonal to that. (For example, you can call a private static method without any instances ever existing...) – Jon Skeet Apr 27 '15 at 06:59
  • 1
    @JonSkeet Actually sir as for as private method is concern-they can call only inside a class.when You can call them as directly by its name, so why bound them with overhead of ClassName.methodName. – TheCurious Apr 27 '15 at 09:04
  • @dubey-theHarcortians: You don't have to specify `ClassName` to call a static method, if you're calling it within the same class (or a subclass). The point is that you should make a method static if it doesn't logically operate on an instance of the class - it's as simple as that. – Jon Skeet Apr 27 '15 at 09:07
  • 1
    exactly sir.I also have same point since a method which is private have same behaviour as prvate+static.each and every property of static method lost its existence in case of private method. Why private+static method's need occurs. Is it for restriction purpose, to restrict access of non static variables inside a method? – TheCurious Apr 27 '15 at 09:21
  • one more thing hopefully you answered in hurry.this static private method is not accessible in subclass :) @JonSkeet as u told-if you're calling it within the same class (or a subclass). – TheCurious Apr 27 '15 at 09:27
  • @dubey-theHarcortians: I was talking about static methods in general. Again, you're trying to tie two entirely orthogonal concepts together, and it's not at all clear to me why you'd wnat to do so. A private instance method does *not* behave the same as a private static method. Hint: try to access instance variables from a private static method... And if you're now going to say there's no point in private static methods: try calling a private *instance* method when you don't have an instance to call it on... – Jon Skeet Apr 27 '15 at 09:33
  • thanku sir.i found following summery-1-you can call this(private+static) method in another static method of same class even before no object is created.2- u can not access, non static instance member of class in it. – TheCurious Apr 27 '15 at 09:41
  • Yes. And that's *exactly* the same as for non-private static methods. As I say, they're orthogonal concepts. The *only* area in which they overlap is that non-private instance methods can be overridden. – Jon Skeet Apr 27 '15 at 10:40

1 Answers1

2

Well, if you don't want to expose a method to the outer world and use it only internally, you can mark them as private. That applies to static methods as well. An example :

public class Sample {

    public static void main(String[] args) {
        SomeTestClass.display1(); // compilation error
        SomeTestClass.display2();
    }

}

class SomeTestClass {
    private static void display1() {
        System.out.println("displaying 1..");
    }

    public static void display2() {
        display1();
        System.out.println("displaying 2..");
    }
}

In the above code, method1() is supposed to be used only in class SomeTestClass. This increases abstraction.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 1
    "Well, if you don't want to expose a method to the outer world and use it only internally, you can mark them as static." No, you mark it as private... – Jon Skeet Apr 27 '15 at 09:34
  • @JonSkeet - I meant to say *private*, not *static*. (A typing blunder nonetheless :P) Corrected it.. – TheLostMind Apr 27 '15 at 10:31