Skip to main content
gamedev.net
🔒 Locked

java : truth about static finals

Started by GekkoCube 5 replies 1.3k views
Original Post
GekkoCube
GekkoCube
what are the advantages and disadvantages of making a class static? and the same question for class methods? and the same question for class member variables? ---------- class CMenu { public static final int m_index = 0; } in the above class, m_index is created when the app start, not when the class is created. thus, my confusion in all this...why would a programmer ever use static final (other than the typical advantages of having a static variable)? does it save memory?
Zipster
Zipster
Static variables are shared across all instances of an object. Also, you don't need an instantiated object to access a static variable.
BobV
BobV
because you can now refer to CMenu.m_index without creating an instance of CMenu.
haggis22
haggis22
I believe the m_index field is created the first time the class is referenced, not when the app in general is stared. If the class is never referenced in the app, the memory will never be allocated.
GekkoCube
GekkoCube
i read an article that said to use static finals as much as you can.

but im wondering...why?

if i have a game class which is destroyed when i go to GameMenu, then i would everything about it to destroy as well - i dont want any lingering static final variables.

so what are the disadvantages of static final's?
GKW
GKW
Static finals used to speed up execution of a program but that is no longer the case. JVM's are now designed to work best with proper object oriented design. If you have an object or variable that should be static or final then make it static or final otherwise don't.
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
dot
dot
In java, for all static final of primitive types, including strings, are 'inlined' in the code. For example
public interface A {   public static final int SomeNumber = 234;   public static final String SomeText = "ABC";}public class B {   public void foobar() {      System.out.println(A.SomeNumber);      System.out.println(A.SomeText);   }}


If you try to disassemble the generated class files, you would have notice that in foobar, there's no longer reference to interface A anymore. Instead, you get 234 and "ABC" inlined in the code.

Now what is this imply?
1) You can move all your 'constants', which are typically static final, into an interface file, compile everything, and delete that .class file, and everything will still run. Proven with my MIDlet packages.
2) You can still have your 'constants' in your original class (in this case, class B). However, a quick disassemble will show that SomeNumber and SomeText still show up as members of class B (which is logical). Thus, if they are primitives and already inlined, these entries are unnecessary and takes up space. A good obfuscation should remove them though.

Note that if however, you have an insane amount of static variables all referenced by the main class at start up, your start up will be slow as well, because the class in question has to be loaded, and allocated memory for the static.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.