• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

A question about constants

 
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

In the OCAJP 7 Summary by Bagdoury Mohammed Amine ( yes, again ) is written :

An interface’s variables are implicitly public, final & static. They should be assigned at declaration. It’s methods are implicitly public and they can’t be final or static.



My question is now, how you can assign a public final static variable in another statement, because there is written "should be assigned" and not have to, but everytime I ever tried to assign such a variable in different statements I got a compiler error.
It would be also nice if somebody could write me in which extract you can read all the stuff with the constants ( that means, when you have to initialize a constant and when not etc. ) in the JLS.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamil Hlubek wrote:My question is now, how you can assign a public final static variable in another statement, because there is written "should be assigned" and not have to, but everytime I ever tried to assign such a variable in different statements I got a compiler error.


That's almost correct! "should" has to be "must" (as you already discovered yourself). Because you can't use a static initializer block in an interface (like you can with a class). This won't compileSo you have to write something like thisBut you could of course initialize this variable using a method from a classThis code uses an instance method, but it would work as well if calculateAbc() would have been a class method.

Kamil Hlubek wrote:It would be also nice if somebody could write me in which extract you can read all the stuff with the constants ( that means, when you have to initialize a constant and when not etc. ) in the JLS.


It's not from the JLS, but I think it's even better (*): this post has a great and understandable explanation about the initialization of class, instance, and local variables (even when they are marked final).

Hope it helps!
Kind regards,
Roel

(*) I wrote that post so probably I'm a little biased
 
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That link is excellent, regardless of the author ;)
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic