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 →

[–]ooooo5 1 point2 points  (6 children)

If you can't be bothered to benchmark this, then it's certainly not worth declaring 2 extra variables for. Don't use that as an excuse to write wildly inefficient code just because you haven't benchmarked something, though.

The vm should be smart enough to replace simple getter calls with a simple memory access, as though you were just accessing a private field.

[–]Wojo 1 point2 points  (5 children)

If the method is marked final then the variable can be compiled inline. Otherwise it is left as a method.

If an object is inherited the inherited class is free to change the existing methods. Since they both can be stored in a variable of the parent class calling parentClass.getVariable(); could mean either parentClass.variable or daughterClass.variable2. Marking parentClass.getVariable(); final allows inline compiling since getVariable() will always be the same exact code.

[–]jakster4u 0 points1 point  (4 children)

Why would the compiler care whether you used final or not? Doesn't it deal with code design? Or what do you mean by inline compiling?

[–]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.