aspose file tools*
The moose likes Java in General and the fly likes Method Inlining in Java 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 » Java in General
Bookmark "Method Inlining in Java" Watch "Method Inlining in Java" New topic
Author

Method Inlining in Java

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Hi,

The question I want to ask is "Are static methods inlined in java ?". And during coding competitions if one declares his methods all Static then how is it going to affect execution time ?

Thanks
user101
Problems And Solutions - Algorithms
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
What do you mean by inlined? Inlined where?

Write a small class with an instance method which calls a static method and inspect its bytecode.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Satyajit Bhadange wrote:. . . . And during coding competitions if one declares his methods all Static then how is it going to affect execution time ?
For the judges, it will make execution time very fast. You can simply fling the whole thing into the “losers” bin
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
By Inlined i meant...if method is static then is code of static method copied to the line where it is called or normal method call is executed.
As in case of final primitives ,wherever i is called.i assume 10 will be replaced in the code during compilation itself.
Does it happen in case of static method calls ?
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
You can simply fling the whole thing into the “losers” bin


what do you mean by above line ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
I would presume any supposedly object-oriented code containing only static methods would fall at the first hurdle.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Satyajit Bhadange wrote: . . . .
As in case of final primitives ,wherever i is called.i assume 10 will be replaced in the code during compilation itself.
I think that happens, yes, for compile-time constants.
Does it happen in case of static method calls ?
Have you tried it? Try it with final methods, as well as static.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Compile that, disassemble it using javap -c -private Test and see the results.

If static methods are not inlined in byte code (which you can see for yourself if they are or not), they may still be inlined by the Just-in-time (JIT) compiler that compiles byte code to native code while the JVM is running. Whether that would happen or not is a lot more difficult to determine. You would need to look at the JVM source code for that, and I doubt you're going to find it easy to do that. (I tried myself once, and didn't like the experience one bit.)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Thanks to both of you.

I will try what you suggested.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Wow.

This
javap -c -private Test
is amazing.

OffTopic : from where did you learn this commands.Can you give link or references.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You’re welcome

I thought, if you knew about inlining, you would know about the javap tool. Start here, and you see a blue band near the top of the page with the names of different tools on. Click on any of them for more details. Note that page is the Java6 version; you can change 6 to 7 in the URL and get the latest version.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Now repeat the exercise with the keyword final omitted and see what difference it makes.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
I knew only little about javap tool, to see the methods of class. Thanks Again.

I tried manipulating method signature with final,static, final static and normal method.

In case of final,static and final static it gives same output.

Also i checked with final instance variable..It replaces value of final variable with actual value.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
That last replacement it your inlining. Try declaring a final variable in another class, and repeat the exercise. Now change the other class, compile it and use javap on your class. There are all sorts of entertaining things you can do with javap.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Okay.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Inlining in Java