• 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

java constants

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read in SCJP Kathy Sierra book that for creating
constants you have to use public static final ....

what is the need to use static modifier? Isn't final enough?
 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using final is enough - but in that case you'll need to create an object of the class, before you can access a constant of it.
If it was both final and static, you can directly access the constant through the class name:
int x = SampleClass.MY_CONSTANT;
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you omit static then you will require an instance first, like Devaka said. However, each instance has a separate copy with the exact same value. For an int constant that requires 4 bytes (size of int) times the number of instances, instead of just 4 bytes in total.

Note that constants don't need to be public. They can be protected or private, or have no access modifier (i.e. they have default (package) visibility).
 
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:If it was both final and static, you can directly access the constant through the class name:
int x = SampleClass.MY_CONSTANT;



is this the only reason to have both static & final modifier? is there any answer related to serialization?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently my entire post means nothing.

As for serialization, if the constant is not static it will also be serialized. That means that there will be 4 bytes* in the serialized form that will always be the same. Talk about wasting space.


* Actually more; 4 bytes for the value plus some overhead that identifies the field, etc.
 
Rajagopal Mani
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which means static filed(s) is ignored by serialization process plus the static modifier increase the usage of memory.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes and no. Yes, static fields are ignored by the serialization process. No, the static modifier does not increase the usage of memory.

If you have no instances then a static field uses more memory than a non-static field. (One occurrence vs none.)
If you have one instance then they use the same amount of memory. (One occurrence vs one.)
If you have more instances, each has its own copy of the field, so the static fields uses less memory. (One occurrence vs multiple.)

In general, as soon as you create more than one instance, the static modifiers actually decreases the usage of memory.
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
One question while reading this post. Apologies diverting from the original question.
Java 5 introduced Enumerations for declaring constants. If we start using Enumeration then we will need more memory in case #3. Isn't it ?
There I guess use of Enumerated constant should be restricted.

Regards,
Amit
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit: No.

Constants in an enum exist only once in memory, just like static fields in a regular class.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper for this information.
I thought because we never declare the enumerated constants as Static it was not the case.
But now after reading you post recollected that they are implicitly final and static.

regards,
Amit
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use the term Enumeration but use enum because there is also an interface called Enumeration.
 
Rajagopal Mani
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everybody for making very good understanding.

Special thanks to Rob for the detailed info.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic