1

Is it an example of inlining?

final class Executive extends Manager
{  
    public raiseSalary(int byPercent)
    {  
        return salary;
    }
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
Arya
  • 13
  • 5
  • 4
    Why do you suspect there might be a relation? – John Dvorak Jun 24 '14 at 08:53
  • i may be mistaken, but i think i read someplace that `final` methods could be inlined as a compiler optimization. Declaring a class final however has no relation to inlining as far as i can tell – omu_negru Jun 24 '14 at 08:55
  • @omu_negru that would be correct. I still suspect a nefarious origin of this question (read: exam) – John Dvorak Jun 24 '14 at 08:55
  • @BoristheSpider not if your fields are public and non-final. Then the compiler cannot assume anything about them. Bonus points if they're volatile. – John Dvorak Jun 24 '14 at 08:56
  • It's given in my book(core java by Cay S. Hostmann) but I can't understand what the Author is trying to say. – Arya Jun 24 '14 at 08:58
  • The second reason for final methods is efficiency. In earlier implementations of Java, if you made a method final, you allowed the compiler to turn any calls to that method into inline calls. Quoted from Thinking in Java – omu_negru Jun 24 '14 at 08:59
  • @omu_negru can you point to an online version of that from an answer, or perhaps find another source? – John Dvorak Jun 24 '14 at 09:00
  • http://www.saeedsh.com/resources/Thinking%20in%20Java%204th%20Ed.pdf... Look for the chapter describing the `final` keyword. As a disclaimer, i do not own that site, it was the first that popped into google when searching for the reference – omu_negru Jun 24 '14 at 09:01
  • `inline` in programming means the same things as in English. It means to place code or data from one place into another place where it is used, usually for efficiency purposes. – Peter Lawrey Jun 24 '14 at 09:48
  • BTW The compiler does almost no optimisations. The main the `javac` does is constant evaluation which is a form of inlining of constant values. It doesn't do much else. – Peter Lawrey Jun 24 '14 at 09:50
  • does inlining moves the code to cache memory? – Arya Jun 24 '14 at 10:06

3 Answers3

3

Is it an example of inlining?

No, it's not.

Is there any relation between final keyword and Inlining?

Kind of. Read this:

Contrary to the implication of many tips, methods declared as final cannot be safely inlined by the compiler, because the method could have a non-final declaration at runtime.

To see why, suppose the compiler looks at class A and subclass B, and sub-subclass C and sees a final method in A which it inlines into C. But then at runtime the versions loaded for A and B are different and the method is not final in A, and overridden in B. Then C uses the incorrectly inlined version.

From http://www.javaperformancetuning.com/tips/final.shtml

Andres
  • 10,561
  • 4
  • 45
  • 63
  • 4
    However, the Hotspot JVM can make a lot of these calls at runtime. – Thilo Jun 24 '14 at 08:59
  • 1
    @Thilo but so it can without the `final` keyword. – John Dvorak Jun 24 '14 at 09:04
  • 1
    That's right. Even non-final methods can be inlined (if the JVM determines they are not being overridden). – Thilo Jun 24 '14 at 09:06
  • @Andres-Would you mind describing your `second para`! I couldn't understand---`But then at runtime the versions loaded for A and B are different and the method is not final in A, and overridden in B`??? If initially the method has been declared final in A!!!Please don't mind,I am still a learner. THANKS. – Am_I_Helpful Jun 24 '14 at 09:31
  • The point is that the code at runtime may not be the same that as compile time. Therefore, inlining cannot be done safely. – Andres Jun 24 '14 at 09:39
1

No, that it's not 'Inlining'.
Inlining is when the compiler decide to replace your function call with the body of the function.
Read here for full example https://stackoverflow.com/a/3925068/3743987

Community
  • 1
  • 1
Azincourt
  • 935
  • 5
  • 13
  • 29
0

Java compiler does not inline final classes or methods. You can check this article.

As I know, inlining is applied to primitive constant statements. For example, the Java compiler replaces all the occurrences of MY_NUMBER constant below with 10 during the compilation phase. Therefor, at runtime, Java does not need to make memory access to read the value of MY_NUMBER.

private static final int MY_NUMBER = 10;
ovunccetin
  • 8,443
  • 5
  • 42
  • 53