Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do static variables, methods impose a penalty

 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a similar thread on the topic.

http://www.coderanch.com/t/202303/Performance/java/Static-vs-Instance
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anupam Sinha:

[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.


It would be true for static methods, too. The stack memory is only reserved for a method *call* - once the method returns, the memory is freed again.

As far as I know, the very only difference between static and instance methods is whether the "this" reference is available.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
sreekanth nair
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you have enough memory in your computer.No performance issue will come up.

Static variable or static methods are written to the memory as soon as complier finds it.

The name itself 'Static' will give the idea about it. So if you purposefully need a class member to be static then only you have to make it as static.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic