Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When do we use static class?

 
Rakesh shankar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I understood the usage of static modifier wrt variable and methods. For example sqrt is a static method which can be used without creating/instantiating any object.
Likewise, can anyone tell me when do we declare class as static?

Thanks!
 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always; unless the class needs access to non-static members of its enclosing class. Think of a non-static inner class as one that has an extra field which has a reference to an instance of the outer class. So you can see, if it doesn't use methods or fields of the outer class, it's just a waste of space, so make it static.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is also a lot easier to gain access to new Outer.Inner() than to new Outer().new Inner()

I think that’s the right syntax, but I’m not certain.
 
Rakesh shankar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

I could not not follow your explanation completely. Could you please elaborate your explanation with an example.

Thanks!
Rakesh
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we may be jumping the gun here. Rakhesh, have you studied nested or inner classes yet? If you haven't, a better answer may be: don't declare a class as static at all, unless and until you have studied nested and inner classes.

After that, if your class is a nested or inner class, and if that nested class does not need any instance data from the outer class, and it's also not an anonymous or local class, then you can (and probably should) make the nested class static.

If the second paragraph above did not make sense to you, reread the first paragraph, and try again after you learn about nested and inner classes.
 
Anurag Malaviya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh shankar wrote:Hi all,

I understood the usage of static modifier wrt variable and methods. For example sqrt is a static method which can be used without creating/instantiating any object.
Likewise, can anyone tell me when do we declare class as static?

Thanks!


Hi Rakesh

You can declare any class as static, whose variables and methods will be used as a common resource, by other classes.
In this way, you don't have to create an instance of a class to use these methods.

Hope this helps.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only declare nested classes static. I think that last post was confusing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic