[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

final member

Posted on 2012-03-16
8
Medium Priority
?
224 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 41

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

650 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