• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

When do we use static class?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Saloon Keeper
Posts: 15727
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79943
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 5059
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only declare nested classes static. I think that last post was confusing.
 
Do not set lab on fire. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic