This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final methods

 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a static method be final or is it implicitly final anyway and compiler doesn't mind if you include final keyword anyway?

Is it true that static methods cannot be overridden but can be redeclared in subclass?

Thanks
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nothing is implied. For instance, you can't use a final method in a static context, just as you can't override a final method. Yes you can use them both.
 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darin

Sorry, didn't understand.

1. Can a static method be final
eg
static final void doStuff()
{
code
}

If so, what does this mean?
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Oliver:

1. Can a static method be final

Originally posted by Darin Niard:

Yes you can use them both.

If it is both static and final, then it takes on the attributes (described in the chart) of both keywords.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that static methods cannot be overridden but can be redeclared in subclass?

I'm about to give a nitpicky answer. Since this was posted in "beginner" it's perhaps best to just ignore this issue. However since you've asked specifically, I'll answer. If other beginners read this thread and are confused: don't worry about it; this is a subtle detail that isn't that important to most people.

It's true that static methods cannot be overridden (whether final or not) because "overridden" implies dynamic polymorphism, and static methods do not have this. E.g.:

This will print "Foo", not "Bar", even though x is really referencing a Bar instance. If you remove the "static" from both doIt() methods, the output will change to "Bar". This is why we say that static methods are not overridden. However, it's obviously possible to declare doIt() in both Foo and Bar, which looks like overriding to many people. The correct term for this is hiding - we say that the method Bar.doIt() hides the declaration of Foo.doIt(). (No, it's not a very obvious term, unfortunately.)

So, coming back to the effect of final: for nonstatic methods, final meant the method cannot be overridden. For static methods (which already cannot be overrridden) final means they cannot be hidden, either. Either way, final means that you can't declare a method in a subclass which has the same name and argument list as a method in a superclass.
 
Pedro Penna
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The final keyword also tells the compiler that the variable is a constant, which allows it to optimize certain areas in your code.



In the example above, if you change the variable x's value to "false", the compiler will remove the "if" statement in the resulting ".class" file, eliminating the overhead caused by its expression evaluation.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darin Niard:
you can't use a final method in a static context


Huh? Could you please elaborate?
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


Huh? Could you please elaborate?

I could have stated it as "non-static" final method, but that should have been assumed.
[ July 22, 2004: Message edited by: Darin Niard ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could have stated it as "non-static" final method, but that should have been assumed.

Errr...

"You can't use a non-static method in a static context" is true.

"You can't use a final method in a static context" means something different, and is not correct. It's entirely possible to have a method which is both static and final, and such a method can easily be called from a static context.
[ July 22, 2004: Message edited by: Jim Yingst ]
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. Clearly you read nothing else I or anyone else posted, and took my statement way out of context.

The guy thought that final and static somehow overlapped eachother, which is false, and if you read what I said with that in mind, it will make perfect sense.
[ July 22, 2004: Message edited by: Darin Niard ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I had read all that, and no, your statement still doesn't make sense to me.
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
"You can't use a final method in a static context" means something different, and is not correct.

Yes it is.

final void m() { }

Are you saying that this can be used in a static context?
Originally posted by Jim Yingst:
It's entirely possible to have a method which is both static and final, and such a method can easily be called from a static context.

Oh really.
Originally posted by Darin Niard:
Yes you can use them both.

Originally posted by Darin Niard:
If it is both static and final, then it takes on the attributes (described in the chart) of both keywords.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigh

final void m() { }

Are you saying that this can be used in a static context?


Certainly not. I'm saying that

static final void m() { }

can be used in a static context. I know that, you know that, great. If you'd said "you can't use a final nonstatic method in a static context" then there would have been no problem. I now understand what you meant, but it was not obvious. I would suggest that if both Ilja Preuss and myself (as well as poor Craig) did not think your meaning was obvious, then perhaps "that should have been assumed" should be "that should have been stated".
[ July 22, 2004: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic