Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2006

Tip: Looking for answers? Try searching our database.

Making JAVA methods non-virtual

Thread view: 
Sukanto - 29 Jun 2006 18:44 GMT
We know that, In Java every method is virtual by nature, i.e. always
late binding is performed.
But early binding helps in improving performance.

Is it possible to change this behaviour to early binding. i.e. can Java
methods be made non-virtual ?
Thomas Hawtin - 29 Jun 2006 19:05 GMT
> We know that, In Java every method is virtual by nature, i.e. always
> late binding is performed.
> But early binding helps in improving performance.

A modern JVM will be happy to inline your virtual methods. Even in C++
that cost of a virtual function is effectively zero.

Look for optimisations (if necessary) at a higher level.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Sukanto - 30 Jun 2006 13:41 GMT
> Look for optimisations (if necessary) at a higher level.

Actually more than optimisation I am eager to know whether this can be
done or not ? If yes, then how ?

I was asked this question in an interview. And, the interviewer told me
that it is possible to do so as well as asked me to explore it.
Andy Flowers - 30 Jun 2006 21:36 GMT
>> Look for optimisations (if necessary) at a higher level.
>
[quoted text clipped - 3 lines]
> I was asked this question in an interview. And, the interviewer told me
> that it is possible to do so as well as asked me to explore it.

Take a look at final, i.e. public final void finalFunc()
Matt Atterbury - 04 Jul 2006 05:03 GMT
> >> Look for optimisations (if necessary) at a higher level.
> >>
[quoted text clipped - 6 lines]
>
> Take a look at final, i.e. public final void finalFunc()

C++ virtual methods implement dynamic binding. Non-virtual methods
implement static binding.

AFAIK all Java methods are dynamically bound, even final ones, since I
can't see how to implement them as dynamically bound in superclasses
of the class but statically bound in the "final" class.

I think I would have flunked that interview as I would have argued
that the interviewer was wrong (:-), but I'd like to know the right
answer :-)

m.
Oliver Wong - 03 Jul 2006 22:30 GMT
>> Look for optimisations (if necessary) at a higher level.
>
[quoted text clipped - 3 lines]
> I was asked this question in an interview. And, the interviewer told me
> that it is possible to do so as well as asked me to explore it.

   Was this an interview for a Java related position? Because AFAIK, Java
itself has no concept of "virtual" methods. Rather, it's a term borrowed
from C++. What I'm getting at, I guess, is that I suspect it's conceivable
for someone to be the worlds greatest Java programmer, and to have no idea
what "virtual methods" are.

   - Oliver
AndrewMackDonna - 03 Jul 2006 23:07 GMT
>>> Look for optimisations (if necessary) at a higher level.
>>
[quoted text clipped - 11 lines]
>
>    - Oliver

I'd word that differently.

All java methods are virtual and can't be none-virtual, and so no
keyword is needed.
Thomas Hawtin - 04 Jul 2006 07:11 GMT
>>    Was this an interview for a Java related position? Because AFAIK,
>> Java itself has no concept of "virtual" methods. Rather, it's a term
>> borrowed from C++. What I'm getting at, I guess, is that I suspect
>> it's conceivable for someone to be the worlds greatest Java
>> programmer, and to have no idea what "virtual methods" are.

> I'd word that differently.

Yup, it's a common case of an interview question depending on incorrect
terminology.

> All java methods are virtual and can't be none-virtual, and so no
> keyword is needed.

Point of pedanty: Not all of them. Private methods, calls to super
methods, static methods, static initialisers, as well as constructors
use different, non-virtual bytecode (invokestatic and invokespecial
instead of invokevirtual and invokeinterface, IIRC). At runtime anything
(consistent with the spec) can happen. Even interface methods
implemented in dynamically loaded classes can be inlined.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

AndrewMackDonna - 04 Jul 2006 21:37 GMT
>>>    Was this an interview for a Java related position? Because AFAIK,
>>> Java itself has no concept of "virtual" methods. Rather, it's a term
[quoted text clipped - 18 lines]
>
> Tom Hawtin

Cheers, I was a bit lazy there...  I was simply referring to public
instance methods, rather than the other types.
Dale King - 05 Jul 2006 08:06 GMT
> A modern JVM will be happy to inline your virtual methods. Even in C++
> that cost of a virtual function is effectively zero.

An old article talked about the fact that the cost of virtual methods in
C++ is basically equal to the cost of adding a parameter. Not to many
people have trouble with adding a parameter to a method but so many
people worry over the "cost" of virtual methods.

Signature

 Dale King



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.