• 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

uninitialized final variable!

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

Final Variables
Declaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialized with an explicit value (notice we said explicit rather than default). For primitives, this means that once the variable is assigned a value, the value can't be altered. For example, if you assign 10 to the int variable x, then x is going to stay 10, forever.


K&B BOOK PG 57.

So does this mean that without initialization, the final variable in the above source gets the value 0 assigned automatically, since its the default for primitves(here int), and receives the ability to modify it later



prints out a's value as 10.


But this

Is this valid.... Compile error is there.
If the value 10 was assigned in the first coding sample. why does the switch statement requires to initialize the variable at the same time??

Compile error :- TestKB.java:9: constant expression required
case b:
^
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The reason is that in switch statements ,the case should always be a compile time constant and in your case
b is not a compile time constant. To have a look about what is a compile time constant, you can have a look
at my blog and you will find a link to a very good discussion about compile time constants.

Check ThisDiscussionOnRanch


HTH,
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to add that the case argument must be of type int but since char, byte, short are compatible with int, they also work fine.

But you may not use long for case argument.
 
Lalaka Jeerasinghe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prithiv!!

The link i followed was really helpful
Thanks for pointing it out.

https://coderanch.com/t/454384/java/java/compile-time-constant
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Lalaka,

You are very welcome.

Happy Preparation,
 
reply
    Bookmark Topic Watch Topic
  • New Topic