• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Inlining in Java

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply fling the whole thing into the “losers” bin


what do you mean by above line ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would presume any supposedly object-oriented code containing only static methods would fall at the first hurdle.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you.

I will try what you suggested.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now repeat the exercise with the keyword final omitted and see what difference it makes.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic