• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the difference between final, static, and final static.  RSS feed

 
kasthala kasturi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
can anyone answer to this question.I know what static member does,and final member does.But please anyone tell me what is the use of declaring final static to a member.Thank you in advance.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO

final - Makes the member value be constant (cannot be changed).
static - All instances of a class share the member.

I'd say,

final + static - All instances of the class share the member and its value cannot be changed.

Hope this helps and answers your question?

-Raj.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you refer to static member variables, then the final keywork is used to create constants.

The variable belongs the class and cannot be altered, therefore it is considered a constant.

If you refer to static member methods, the final keyworkd avoid that other classes in the hierachy shadow the original method declaration.

You know that static members are not inherited, therefore static methods are not inherited either. But a subclass could shadow a static method declaration in the parent class, unless the static method is declared final.
 
KellyDeek Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what will happen if we override a static method in a subclass that extends the superclass...

Is it possible?..Are there static local variables in a mother class method which can be extended and changed?
 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@KellyDeek Jones: It is possible to override static method, but IMO, static methods should be accessed in a static way.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KellyDeek Jones,
Your post was moved to a new topic.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KellyDeek Jones wrote:So what will happen if we override a static method in a subclass that extends the superclass...

Static methods cannot be overridden. See Overriding vs Hiding.

KellyDeek Jones wrote:Is it possible?..Are there static local variables in a mother class method which can be extended and changed?

There is no such thing as static local variables. Local variables would be unaffected and inaccessible by any overriding method, or any hidden method, in another class. Static variables would be accessible from another class, unless they're private. Just like non-static member variables.
 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mike Simmons: You are right. Thanks for the article. Good knowledge.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!