Solved

final member

Posted on 2012-03-16
8
202 Views
Last Modified: 2012-03-20
Does declaring a class member as final cause it to take less memory or cause any processing using it to be faster?
same for final methods or classes?
0
Comment
Question by:allelopath
8 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 37729241
I don't think, final is nothing to do with memory/speed!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37729243
*nothing - anything
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 84 total points
ID: 37729320
The differences are probably very small, but yes there could be performance improvements. Any extra information you give the compiler, gives it a chance to optimize the bytecode more aggressively.

Essentially, because of this, but more importantly to help reduce bugs, you should use final everywhere where it is appropriate, it just gives the compiler more info and therefore more of a chance to pick up errors and to optimize.
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 83 total points
ID: 37729348
It doesn't unless you are multithreading (making it visible to all threads, assigning only once) and for clearer design.

http://stackoverflow.com/questions/4279420/does-use-of-final-keyword-in-java-improve-the-performance
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Assisted Solution

by:gudii9
gudii9 earned 83 total points
ID: 37730900
Check following interesting explanation, link

Use private and static methods, and final classes, to encourage inlining by the compiler.

http://www.javaperformancetuning.com/tips/rawtips.shtml
0
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 167 total points
ID: 37733629
For multi threaded code using final makes a huge difference in performance.  This isn't because of anything that's happening to the variable, it's because you don't need to lock the threads before you access the variable:

public class SlowClass {
    private int m_Value ;

    public synchronized int getValue() { return m_Value ; }
    public synchronized void setValue(int n) { m_Value = n ; }
}

public class FastClass {
    private final int m_Value ;

    public FastClass(int n) {
        m_Value = n ;
    }

    public int getValue() { return m_Value ; } // NOTE: Not synchronized
}

In a multi threaded situation SlowClass may run 100x slower than FastClass because there's no requirement to lock on the class before accessing the data.

This is when final really makes a difference.

It can also help the compiler produce slightly more optimized code (when applied to the class or member level) but those won't get you order of magnitude benefits, as the multi threading final will.

Doug
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 83 total points
ID: 37736234
>>For multi threaded code using final makes a huge difference in performance

the diff is between sync/non-sync code, nothing to do with final.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 167 total points
ID: 37736264
>> the diff is between sync/non-sync code, nothing to do with final.

OK - technically correct it's not the "final" keyword that makes the performance difference, it's the different coding pattern that the use of final produces (i.e. immutable objects) which in turn generates the huge performance difference.

Doug
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now