• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Final instance varible

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

I am not understanding one thing realted instance

variable.Bydefault value of instance varible(int) is 0; but if i

make it as

final int x then i have to explictly assign the defalut value like

final int x=10;
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes final variable must be initialized before Use.
final int x=10;
or final int x; int x=10;
because final variable defines a constant.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

or final int x; int x=10;



That is invalid .
final variables need to be initialized while being declared.
Dont cite wrong examples !
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


final int x; int x=10;



This doesnt even compile because you are defining the same variable twice in a program. if you want, you can declare a final variable then assign later like this:
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikrant Sahdev:
That is invalid .
final variables need to be initialized while being declared.



I don't think I agree. For example, this program compiles and runs:

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

Originally posted by Brian Cole:


I don't think I agree. For example, this program compiles and runs:




i think virkant's way referring to instance variables, where 0 is assigned implicitly. your x variable is not an instance so has no value and therefore can be reasigned a new value once.
 
Brian Cole
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Raymond Jr.:
i think virkant's way referring to instance variables, where 0 is assigned implicitly. your x variable is not an instance so has no value and therefore can be reasigned a new value once.



I still don't think I agree. Try this program that compiles and runs:



[edit: Ok, it doesn't run because it has no main(), but you know
what I mean. Btw, you can check out blank finals in the JLS.]
[ September 20, 2007: Message edited by: Brian Cole ]
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final just means that once the reference variable has been assigned it cannot be re-assigned. You do not have to assigne it a value when you declare it.
 
Michael Raymond Jr.
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:


I still don't think I agree. Try this program that compiles and runs:



[edit: Ok, it doesn't run because it has no main(), but you know
what I mean. Btw, you can check out blank finals in the JLS.]

[ September 20, 2007: Message edited by: Brian Cole ]



well, you are indeed correct.

i did it in a main() program (not just a clas) and made the variable static and it would not work, so I wrongly assumed it would not work for a non-main class as well.


so, you can reasign it even so it has a default value in a class without main(), but not a class with main() (regardless of static or not btw).






very strange behavior
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final instance variables must be initialized before constructor completes - so K&B book says. Brian's code is changing final value in a constructor. Try doing it in an instance method.
 
Michael Raymond Jr.
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jan Nowak:
Final instance variables must be initialized before constructor completes - so K&B book says. Brian's code is changing final value in a constructor. Try doing it in an instance method.



oh god, how did i miss that!!! ;-)))


didn't even see the name of the method was the same as the class in code.
 
Straws are for suckers. Now suck on this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic