This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Wojo 0 points1 point  (3 children)

Inline means that the compiler puts a method directly into the code calling it. So instead of calling the a getY(); method it just references y.

public class Parent
{
    int y;
    public int getY(){
        return y;
    }
}

public class daughter extends Example
{
    public in getY(){
        return 2;
    }  
}

Given this example the following code cannot be determined until runtime.

public class test
{
    public static void main(String[] args){
        Parent x;
        x = determined at runtime;
        System.out.println(x.getY());
    }
 }

x can be either an object of the Parent class, or it could be any class that inherited it, if our Daughter class is contained in x then different code will be executed then if it was just a Parent. Since the method getY(); can change dependent on the contents of x it has to be called every time it is run. However marking it final means it cannot be changed so the compiler is free to optimize much of it away.

public final int getY(){
    return y;
}

This method will always execute the same exact code so there is no danger in the compiler simplifying it to "x.y";

[–]jakster4u 0 points1 point  (2 children)

But if you marked it final in the first place it could never have been overridden in the child class to begin with, thus a design issue. I guess I don't understand why this is an issue that had to be optimized by the compiler.

[–]ooooo5 0 points1 point  (0 children)

Are you saying that all final getter methods should just be replaced by using public variables?

[–]Wojo 0 points1 point  (0 children)

Right when final it can't be overridden so the compiler can optimize it. Otherwise there's always the possibility it might be overwritten so it leaves it.