Does making final field static improve performance or save memory?

Are final fields given value at compile time by the compiler?

private final String FINAL_FIELD = "31";

Does making final field static improve performance or save memory?

[188 byte] By [deepu-jaina] at [2007-11-27 11:46:49]
# 1

> Does making final field static improve performance or

> save memory?

Yes, it might save memory because you'll only have one per class and not one per instance. Has nothing to do with final and all to do with static.

CeciNEstPasUnProgrammeura at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 2

Here are some really valuable performance tips for you.

- Don't use classes, do everything in one main method. Saves on class loading time and memory usage.

- Don't use Strings use char arrays directly.

- Don't use Lists use fixed size arrays.

- Don't bother with Servlets, that just extraneous layers! Just do everything, including all your JDBC code right in the JSP

cotton.ma at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 3

I wish they would stop worrying about performance and actually learn how to design a program. What good is fast-executing ****?

CeciNEstPasUnProgrammeura at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 4

> Does making final field static improve performance or

> save memory?

It will use less memory, but that's NOT why you do it. You make a variable static if it makes sense for there to be one copy associated with the class as a whole rather than separate copies for each instance.

jverda at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 5

On m_cotton comments:

I am just asking questions and i feel there is no harm in doing so, If you are not intersted to answer its fine with me someone else will answer.

Thanks

Deepak

deepu-jaina at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 6

Actually it's final static fields with primitive or String type that are treated specially. For other final fields only extra compile time checking is produced, they are the same at run time.

final static primitives are treated as compiler constants, and a literal value is substituted. No actual field is created.

Myself I consider an awareness of Java internals valuable. For example, if you were unaware of the above the occasional misbehaviour of final static primitives referenced in other classes would be baffling. If your code references a final static primitive from another class it's compiled as a literal, not a reference, so if their value changes in the other class, the referring class will retain the old value if not re-compiled.

malcolmmca at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 7

hi malcolmmc,

Can you point to me the doc or some web page that has more details on what special treatment is given to primitives and strings.

Thanks

Deepak

deepu-jaina at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...
# 8

If you're interested in this stuff, I suggest you get hold of a book called "Inside the Java 2 virtual machine".

malcolmmca at 2007-7-29 18:09:41 > top of Java-index,Java Essentials,Java Programming...