File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes When do we use static class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When do we use static class?" Watch "When do we use static class?" New topic
Author

When do we use static class?

Rakesh shankar
Greenhorn

Joined: Nov 12, 2010
Posts: 14
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

Joined: Sep 20, 2010
Posts: 3646
    
  16

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

Joined: Oct 13, 2005
Posts: 38756
    
  23
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

Joined: Nov 12, 2010
Posts: 14
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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jul 01, 2009
Posts: 19
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

Joined: Oct 13, 2005
Posts: 38756
    
  23
You can only declare nested classes static. I think that last post was confusing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When do we use static class?