• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why I should go for static class in Java?  RSS feed

 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason that i should choose static class instead of inner class.? can you give any good reason.
 
Stefano Carniel
Greenhorn
Posts: 27
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One example that come into my mind is grouping a set of utilities functions that can be invoked frequently and does not require the instantiation of an object. A typical example is the Math class, which includes lot of static functions.
 
Giovanni Montano
Ranch Hand
Posts: 428
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran madhan wrote:What is the reason that i should choose static class instead of inner class.? can you give any good reason.

A Static class cannot call non-static fields in the class, with an inner class you can have access to all the instances ( of course not the ones inside the methods)

Become more complex if we are speaking of static inner classes, but let's keep simple at the moment.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Math class is not a static class. It's a top-level class.

A static class is one that is nested inside another class, and has the static modifier.

Static nested classes work the same way as top-level classes. That's why you should use them by default. Inner classes (i.e. nested classes that don't use the static modifier) are different in that they have a reference to an instance of the outer class. That means you can only instantiate them when you have an instance of the outer class. You should only use them if the inner class needs access to the instance fields and methods of the outer class.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stephan says, you can use a public static nested class rather as you would a top‑level class. A public inner class however can only be created by something like this:-
Foo.Bar fooBar = new Foo(...).new Bar(...);
or some other reference to an instance of Foo.
Search for the free download of Bruce Eckel's Thinking in Java (try mindprod.com first) which has a chapter about nested classes and explains that problem nicely. I think only the JDK1.4 edition is available free nowadays. But you may be able to find a copy of a newer edition which somebody has bought.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your great answers...but can you give any real world example for using a static class inside another class, so that enclosing class object will use static members or static class becomes a member of it...can you add any real world example.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't add a static class so it can access static members of the enclosing class. You use static classes so you can use the namespace that the enclosing class provides. For instance, a chess piece has a color, but so does a rainbow. The colors used for rainbows are not valid for chess pieces.

Now, we could create a class ChessPieceColor, but it's kinda annoying to use such a long name when the context is clear. After all, a program that deals with chess pieces will probably not reference rainbows that much. Since the color of a chess piece is strongly related to the chess piece, we can just use the ChessPiece class as the namespace for the Color class:

In our program we can just refer to Color, and when ambiguity arises, we can resolve it by referring to Color as ChessPiece.Color. Much more elegant than using the fully qualified name.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot dear Stephan van Hulst....! I understood your great example.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!