inline methods

Discussion in 'Java' started by G. Ralph Kuntz, MD, Oct 5, 2006.

  1. 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)?
     
    G. Ralph Kuntz, MD, Oct 5, 2006
    #1
    1. Advertising

  2. G. Ralph Kuntz, MD

    Chris Uppal Guest

    G. Ralph Kuntz, MD wrote:

    > 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
     
    Chris Uppal, Oct 5, 2006
    #2
    1. Advertising

  3. G. Ralph Kuntz, MD

    Oliver Wong Guest

    "G. Ralph Kuntz, MD" <> wrote in message
    news:...
    > 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
     
    Oliver Wong, Oct 5, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    751
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    489
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    473
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    1,949
    Kevin Bracey
    Nov 21, 2003
  5. Kenneth McDonald
    Replies:
    5
    Views:
    345
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page