• Post Reply Bookmark Topic Watch Topic
  • New Topic

java constants  RSS feed

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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?
 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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;
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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).
 
Rajagopal Mani
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!