Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A final class makes its variables and methods as final
as well, since a final class cannot be extended.
Is the above statement true??
I think the above statement is true for final class methods.
But not for variables.
Thanks!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. final class - can not be extended.
2. final method - can not be overridden.
3. final variable - value can not be changed.
They are three different things, independent of each other except for the following.
A final class does not have to declare a method as final method, since the class can not be extended there is no question of overriding. If it declares, it is perfectly legal.
[This message has been edited by vasansrini (edited August 15, 2000).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avn- right. The methods of a final class are implicitly final, but not the fields.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think the above statement is true for final class methods.

I was confused about this statement. Why only "class methods", why not final instance methods ?
or we dont have anything called class methods. I thought class methods are nothing but static methods.
Jim ,
Help me.
 
Tushar Kansara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The process of declaring a clas Final would no doubt dis allow any of its, class methods(Read Static)& Instance methods not to be overridden. But I am not sure about, The variables.
Say I have an static variable in a class declared final, can it be changed, when i am using an instance of the class & try to change the value in the variable. What about instance variables that have declared within the final class.
Regards
Tushar Kansara
------------------
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of the static and instance variables (which are not final) in a final class can be changed.They perform just
like variables in a class.
U cant override only the methods of the final class.
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A method can be declared final to prevent subclasses from overriding or hiding it."
Static methods are hidden. !!
Instance methods are over-ridden. !!
It is a compile-time error to attempt to override or hide a final method.
A private method and all methods declared in a final class are implicitly final, because it is impossible to override or hide them.
It is permitted but not required for the declarations of such methods to redundantly include the final keyword.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic