As the static methods and variables are loaded even when there is no class instance loaded and probably remain in the JVM till it shuts down. So do these variables and methods impose a performance penalty with respect to there non static counterparts. I am talking of scenario when there are probably hunderds of objects and each living a very short life.
I doubt very much that you could measure it even if there is a penalty. Think about it - what are your alternatives? All of the constants that might be used to initialize instance variables have to be part of the class object anyway.
The byte code of methods is only loaded once - when the class is loaded - anyway, and local variables live on the stack, that is, their memory is allocated when the method is called. There is no difference in this respect between static and non-static methods.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Apr 13, 2003
Thank you William and Ilja. I also thought it the same way. But your posts gave way to some other questions
[WB] All of the constants that might be used to initialize instance variables have to be part of the class object anyway.
But won't that be shortlived. Let say the frequency usage of the static method class is very less. So if I do invoke this class let say after x CPU cycles won't instance vars. be more beneficial.
[IP] local variables live on the stack, that is, their memory is allocated when the method is called.
It gives me all the more reason to use instance methods. Once the stack is over everything is over for that method, but the same wouldn't be true for static methods.
Author and all-around good cowpoke
Joined: Mar 22, 2000
But won't that be shortlived.
No, once the class is loaded, there they sit in memory.
That includes the bytecodes (or machine code if the JIT compiler has run) - not matter how many instances of the class are created, they ALL use the same bytecodes so there is no difference with static methods.
Choosing between static methods and variables versus instance etc is more related to how the class is used than to performance.
I hope you are seeing the difference between an instance of the class definition and an instance of the class. Both of these are runtime objects.
If a variable is defined as belonging to an instance of the class, then it dies when the instance of the class dies. If a variable is defined as belonging to an instance of the class definition (a static variable) then it dies when the class definition instance dies. Which happens when the class definition is unloaded.
The only difference here is scope.
As to thread local memory, in this case 'stack' memory, all variables that are to be worked on will be placed on the stack. It does not matter if its a class variable or an instance variable. A variable is a variable. In this respect, there can be no performance difference. [ March 12, 2007: Message edited by: Mr. C Lamont Gilbert ]