Solved

final member

Posted on 2012-03-16
8
200 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 39

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

17 Experts available now in Live!

Get 1:1 Help Now