• 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 - question 4

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3.
class checkfinal
{
public static void main(String[] args)
{
B b = new B();
b.call();
System.out.println("Hello World!");
}
}
class A
{
final int i = 10;
};
class B extends A
{
A a = new A();

//int i = 100;//redeclaration allowed.

void call()
{
//int i = 20;//redeclaration allowed
i = 25;// Can't assign a value to a final variable: i
}


};
//This also works fine.

DOUBT: HERE REDECLARATION ANY NO OF TIMES IS ALLOWED. THEN WHY IS REASSIGNMENT NOT ALLOWED? IS IT THAT FINAL VARIABLES CAN BE REDECLARED ANY NO OF TIMES?
Thanks in advance
Padmini
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example you showed that even though the variable "i" COULD be inherited that does not stop you from declaring a DIFFERENT variable also named "i". If you DO declare a different "i" variable, then you effectively HIDE the fact that you could have inherited the other one (the "final" one).
Of course when you commment out the declaration of the second "i" then you go back to inheriting the first one, which just happens to be final. You can not change the value of a final variable, so the compiler complains when you try to set it's value to 25.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic