Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why have public static class inside a class

I was going through some code and I saw this:

public class A {
    public A(SomeObject obj) {
      //Do something
    }
    //Some stuff
  public static class B {
    //Some other stuff
  }
}

I was wondering since even the inner class is public why have it as nested and not a separate class? Also, can I do this here: new A.B(SomeObject) ? I feel this defeats the purpose of a static class but I saw this implementation as well so wanted to know.

like image 743
noMAD Avatar asked Aug 22 '12 16:08

noMAD


People also ask

Why do we need static inner class in Java?

Java static nested class A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name. It can access static data members of the outer class, including private.

Can we have static class inside a class?

Note: In Java, only nested classes are allowed to be static. Static nested classes are associated with the outer class. To access the static nested class, we don't need objects of the outer class.

When would you use a public static class?

In Java, the static keyword is primarily used for memory management. We can use the static keyword with variables, methods, blocks, and classes. Using the static class is a way of grouping classes together. It is also used to access the primitive member of the enclosing class through the object reference.

Why static nested class is required explain with example?

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.


2 Answers

I was wondering since even the inner class is public why have it as nested and not a separate class?

That's really a matter to ask whoever wrote the class. It can allow the outer class to act as a "mini-namespace" though - if the nested class is only useful in the context of the outer class, it seems reasonable. It indicates deliberate tight coupling between the two classes. I most often see this in the context of the builder pattern:

Foo foo = new Foo.Builder().setBar(10).build();

Here it makes sense to me to have Foo.Builder nested within Foo rather than as a peer class which would presumably be called FooBuilder.

Note that it also gives some visibility differences compared with just unrelated classes.

Also, can I do this here: new A.B(SomeObject) ?

No, because B doesn't have a constructor with a SomeObject parameter - only A does (in the example you've given).

I feel this defeats the purpose of a static class

You should try to work out exactly what you deem the purpose of a static class to be, and in what way this defeats that purpose. Currently that's too vague a statement to be realistically discussed.

like image 178
Jon Skeet Avatar answered Nov 15 '22 17:11

Jon Skeet


You would have an inner class like this so

  • you can keep an class which only exists to support the outer class encapsulated.
  • you want to be able to access private members of the outer class or other nested classes.
  • you want a nested class with static fields (a weak reason I know ;)
  • you have a class with a very generic name like Lock or Sync which you wouldn't want to be mixed with other classes of the same name used by classes in the same package.

can I do this here: new A.B(SomeObject) ?

You can.

I feel this defeats the purpose of a static class

It takes getting used to but once you start you may have trouble not turning your entire program into one file.java ;)

like image 30
Peter Lawrey Avatar answered Nov 15 '22 16:11

Peter Lawrey