• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Can some explain it to me please?

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test028 extends Object
{
final int y; //1

public Test028() {
y = 0; //2
}

public Test028(int val) {
y = 1; //3
}
}
It just compiles and runs fine.
Explanation gives for the answer is little unclear to me
Can some one share your thoughts pleaseeeee?
thankx
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raghu,
this is what i think,
final int y;
since this is not a static variable,either it should be given value in the decalration.
i.e. final int y=7;
or should be given value in every constructor.
But if it is static, it can be given value in static block.

[This message has been edited by leena rane (edited September 05, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu,
I am not sure of what exactly your question is...but go through this and see if it helps.

The value of Final variables cannot be changed once initialized.But they can be declared as "y" has been at //1 in your code(Add Leena's Explanation here ...easy for me). I have made a few modifications to your program...Do take a look.

Another point is...If you don't comment out either //*4* or //*5*, you'll get a compile-time error. To be more precise, having them both means that you are invoking //*5* after //*4* has been executed, thus trying to reset the value of y (which is illegal).
Cheers!
Shyam
[This message has been edited by Thomas Paul (edited September 07, 2001).]
[This message has been edited by Shyamsundar Gururaj (edited September 08, 2001).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this line:
test a = new test(3); // *5* This statement when used will print out 0.
will print 1 and not 0



[This message has been edited by Thomas Paul (edited September 07, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn is right...
That was a typo...sorry about that.
Thanks
Shyam
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic