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

Final variable return zero before initialize

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

I have doubt about the following program



I know java does not initialize the final variable instead it expect us to provide the value.

But in the above case why it is returning j value as zero.

How come in line 2 j value becomes zero?

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

Actually, all member variables are defaulted to null for references, false for booleans, and 0 for other primitives, regardless of being final. Since your method doesn't actually change the value of j (it just assigns the value of itself to itself), your answer is 0.

John.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final variable gets the value before the constructor finish the execution .so you assign the method inside the constructor

final int j;


Hope this helps
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:Siva:

Actually, all member variables are defaulted to null for references, false for booleans, and 0 for other primitives, regardless of being final. Since your method doesn't actually change the value of j (it just assigns the value of itself to itself), your answer is 0.

John.



But i have read somewhere that java does not assign default value to final variable because if it assigs default value then it can not be changed,so it expect us to provide the value.

Could you please explain this?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siva:

Well, your program ran, so whoever wrote that was incorrect. If Java didn't assign a default, you'd probably get a random number. I did a test to check all three cases:

I consistently get this result:

So clearly, final variables get assigned defaults.

John.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John for your valuable information.

I appreciate it.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siva:

Glad I could help.

John.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic