inline methods

  • Thread starter G. Ralph Kuntz, MD
  • Start date
G

G. Ralph Kuntz, MD

According to the Java language specification, a method can only be
"inlined" by the compiler if it is declared final (necessary, but not
complete requirement).

If I declare a class to be final, this implies that all of the methods
are final. Will javac inline the methods (if other conditions are met)?
 
C

Chris Uppal

According to the Java language specification, a method can only be
"inlined" by the compiler if it is declared final (necessary, but not
complete requirement).

If I declare a class to be final, this implies that all of the methods
are final. Will javac inline the methods (if other conditions are met)?

As far as I know javac /never/ inlines method calls (and would be in error if
it did so).

The real compiler, on the other hand (the one that runs at runtime), is able[*]
in principle to inline /anything/, whether private, final, or anything else.

[*] "able" both legally (i.e. it is allowed by the JLS and JVM spec) and
technically (i.e. it has the technology, and does use it.)

-- chris
 
O

Oliver Wong

G. Ralph Kuntz said:
According to the Java language specification, a method can only be
"inlined" by the compiler if it is declared final (necessary, but not
complete requirement).

If I declare a class to be final, this implies that all of the methods
are final. Will javac inline the methods (if other conditions are met)?

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#14844
<quote>
A private method and all methods declared immediately within a final class
(§8.1.1.2) behave as if they are final, since it is impossible to override
them.
</quote>

That said, your statement may be a bit misleading. javac will inline
methods only very rarely:

<quote>
Such inlining cannot be done at compile time unless it can be guaranteed
that Test and Point will always be recompiled together, so that whenever
Point-and specifically its move method-changes, the code for Test.main will
also be updated.
</quote>

The much more common case will be the JVM, and not javac, doing the
inlining.

<quote>
At run time, a machine-code generator or optimizer can "inline" the body of
a final method, replacing an invocation of the method with the code in its
body.
</quote>

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top