• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to make a class holding state static?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am a fairly experienced C# coder, but new to Java. My question is a general programming question:

- Usually a class which has no internal state (ie fields) is a good candidate for being made static (So doesn't change its reciever class). However, is there ever a reason to make a class holding internal state (fields) a static class? Any examples?

Thanks
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no stand-alone "static" classes in Java.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The C# "static class" concept isn't a first-class concept in Java; i.e, there's no way to explicitly declare one, and the idea isn't used much in Java (you can emulate one by giving your class only a private constructor and then never using it.) Frankly, it's just a mechanism for writing in a non-object-oriented style, and used only in fairly obscure cases like the java.lang.Math class, which holds a bunch of trignonometric and other free math functions. Classes like this lead to brittle, nonmodular code that's hard to test.

So my answer to your question is "no" -- there's never a good reason to do that. Put the state into instance methods, and make the methods instance methods, and that's that!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.
Static keyword with outer class is used more rarely in java,and in most of the time used with inner classes.
which you got to know once you studied inner classes
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:Static keyword with outer class is used more rarely in java...


In fact it is not allowed at all as pointed out earlier.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes.how can it skip from my mind that it is not applied to outer class
for outer class only public,abstract and final is permitted.......

Thanks for pointed out the mistake from my Quote
reply
    Bookmark Topic Watch Topic
  • New Topic