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
.