Solved

final member

Posted on 2012-03-16
8
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 27

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 27

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

740 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