aspose file tools*
The moose likes Beginning Java and the fly likes static final methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static final methods" Watch "static final methods" New topic
Author

static final methods

Craig Oliver
Greenhorn

Joined: Aug 25, 2003
Posts: 21
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

Joined: Jun 08, 2004
Posts: 118

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

Joined: Aug 25, 2003
Posts: 21
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

Joined: Jun 08, 2004
Posts: 118
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Pedro Penna
Ranch Hand

Joined: Jun 05, 2004
Posts: 46
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

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Darin Niard:
you can't use a final method in a static context


Huh? Could you please elaborate?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jun 08, 2004
Posts: 118
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

Joined: Jan 30, 2000
Posts: 18671
Actually I had read all that, and no, your statement still doesn't make sense to me.
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
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

Joined: Jan 30, 2000
Posts: 18671
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static final methods