Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why have public methods inside private classes?

Tags:

java

I was going through a part of a code which was something like this

// compare points according to their polar radius
public static final Comparator<Point2D> R_ORDER = new ROrder();
.
.
.
private static class ROrder implements Comparator<Point2D> {
    public int compare(Point2D p, Point2D q) {
        double delta = (p.x*p.x + p.y*p.y) - (q.x*q.x + q.y*q.y);
        if (delta < 0) return -1;
        if (delta > 0) return +1;
        return 0;
    }
}

Why do we have such public methods inside private static classes. What harm would it do if i made ROrder

  1. Non-Static
  2. Public
like image 580
Somjit Avatar asked Sep 18 '13 09:09

Somjit


People also ask

Can we have public methods in private class?

Yes. Well, obviously you can access those methods from within the same class.

Should public methods be above private?

The best practice is to be consistent. Personally, I prefer putting public methods first, followed by protected methods, following by private methods. Member data should in general always be private or protected, unless you have a good reason for it not to be so.

Should class methods be public or private?

Generally you should expose as little as possible and make everything private that is possible. If you make a mistake and hide something you should be exposing, no problem, just make it public.

Why do we use public in class?

public is a Java keyword which declares a member's access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final .


1 Answers

ROrder Non-Static

By making it non-static you will need the instance of the container class to create the instance of ROder, which maybe due to the design of the class would not make logic. You should keep class non-static only when you really need the instance of outer class to get the instance of inner class.

ROrder Public

Again because they wanted to restrict the use of ROrder outside the context of this class. They did not want any client code or other code to freely create instances of ROrder, as they would not be of any use.

Why do we have such public methods inside private static classes.

In this case because you are implementing an interface Comparator and you will pass this comparator for other uses, such as sorting and you would want the Collections class to have the visibility of compare method, so the method has to be public even if the class implementing the interface is private.

So this is just a logical way to enhance the readability and intent of use of the code.

Logical Use

This class wants the string to be in some format.

public class SomeClass{

     private static class StringHelper{
          //will do the task of parsing and validating that string object
     } 
}

Now in this case you would not want to keep StringHelper class public, as its use is too localized to be reused. So you would rather emphasize that by keeping it private. And there can be methods that are public if StringHelper implemented some interface.

UPDATE:

You should keep class non-static only when you really need the instance of outer class to get the instance of inner class.

On that I think the answer can be too broad, but I would try to explain in short. By that what I mean was that if the inner class object shares some state of the outer object on which its processing is dependent, then you will need the object of outer class to share its state with the inner class object, but if the inner class instance is independent of the state of outer class, then it is safe to keep the inner class static.

like image 200
Narendra Pathai Avatar answered Oct 28 '22 06:10

Narendra Pathai