?
Solved

final member

Posted on 2012-03-16
8
Medium Priority
?
218 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 36

Assisted Solution

by:mccarl
mccarl earned 336 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
Independent Software Vendors: 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 332 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 332 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 28

Assisted Solution

by:dpearson
dpearson earned 668 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 332 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 28

Accepted Solution

by:
dpearson earned 668 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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