• 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

final when used in constructor

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


output:it compiles and runs successfully without warning or errors.


here i was expecting a compiler error at line1 as final variable q is not
initialized,but its working fine. when there was no constructors
it was giving a error so whats happening here.... please explain

Thanks in advance
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final members instance variables can be left blank during declaration , but it has to be initialized before the constructor of the class completes. So in your case the first constructor takes care of the variable q and initializes to 1. if you comment out the line in the constructor, it won't compile.

Thanks , Hope this helps
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is final, it can assigned only once.
The code you've shown does just this.

You cannot change the variable afterwards.


Bu.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A blank final instance variable must be initialized when an instance is created, or the code will fail to compile.
This can be done either in an instance initializer Or in every constructor but not both.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahmed can you explain little more about initialize or in every constructor? Also in the code at line-1 you are reassigning the final variable. Then why code compiles? Even x is not declared anywhere in the code?

 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By initializing a blank final variable in every constructor you ensure that it gets a value when an object is constructed. Alternatively you can initialize it in an instance initializer block in this case you cant initialize in any constructor because instance initializer block run once when an object is constructed.
Also when inistantiating an object you only call one constructor so the variable will be assigned a value once.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly

ftest() { q=1; }
and
ftest(int x) { q=x;//line 1 }

Above are 2 overloaded constructors.

Remember All final variables can be given explicit values only once.
Once given an explicit value if you try to reassing a value to a final variable it will thorough you an error.

You can create objects of this class using

new ftest();
or new ftest(10);

thats why we have initialized q in both the constructors but this is not a reassignment as both are declared in different constructor
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got both you Ahmed & Yogesh. But I just want to know more about it. As I know we cannot change final variables value. That's what we are doing in second constructor.
Suppose final int i=10;
if any method is doing like i++, it gives compiler error. Because method changes final variable's value. Why we are able to do it in constructor?
Can you clear my doubt?
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First remember its blank, so it needs a value when constructing an object of that class and constructors are the most appropriate place to initialize such variable, because they run when an object is being instantaited.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Here " q" is a final variable and when it is assigned by the value 1,how come it is blank variable.And final variables can't be reassigned as they are final but in 1st constructer i=1 and in second constructer we are assinging i =x(i.e whatever the x value is,may be 2,3,4..)though i=1 already so it is opposing the final's rules.
please explain.
shweta
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

we can initialise the final variables before constructor loads.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic