Using private static methods [duplicate]

A private static method by itself does not violate OOP per se, but when you have a lot of these methods on a class that don’t need (and cannot*) access instance fields, you are not programming in an OO way, because “object” implies state + operations on that state defined together. Why are you putting these methods on that class, if they don’t need any state?

(*) = In principle, due to the class level visibility in Java, a static method on a class has access to instance fields of an object of that class, for example:

class Test
{
  int field = 123;

  private static void accessInstance(Test test)
  {
    System.out.println(test.field);
  }
}

You need to pass in the reference to an instance (this pointer) yourself of course, but then you are essentially mimicking instance methods. Just mentioning this for completeness.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)