2

An instance variable is one per Object, every object has its own copy of instance variable.

A static variable is one per Class, every object of that class shares the same Static variable.

class MyStaticClass{
  private static int myStaticInt;

  public static int getMyStaticInt() {return myStaticInt;}
}

class MyInstanceClass{
  private int myNonStaticInt;

  public int getMyNonStaticInt() {return myNonStaticInt;}
}

Is there a performance difference between either? Is it more expensive to call one over the other?

int i = MyStaticClass.getMyStaticInt();

OR:

int i = new MyInstanceClass().getMyNonStaticInt();
Community
  • 1
  • 1
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

2 Answers2

3

It's not a matter of performance. static and instance variables have a different purpose.

Using

int i = new MyInstatnceClass().getMyNonStaticInt();

is almost certainly useless, since each time you call new MyInstatnceClass() you create a new MyInstatnceClass instance, having a new myNonStaticInt instance variable. Since you are not keep a reference to the created instance, you cannot retrieve the same instance variable twice, which makes it useless.

If you need a single copy of a variable to be shared across all instances of the class, static variable is the way to go.

That said, the latter call is also more expansive, since it involves creation and initialization of an instance of your MyInstatnceClass class (in addition to loading and initialzing the class if it's the first access that class).

On the other hand, MyStaticClass.getMyStaticInt() only loads and initializes the class MyStaticClass if it's the first access of that class. It doesn't have to create any instance of that class.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • @Program-Me-Rev Changing the name of the variable and getter method doesn't affect my answer. `int i = new MyInstatnceClass().getMyNonStaticInt();` still doesn't make sense. – Eran Oct 22 '17 at 09:04
  • Thanks for the reply @Eran. I made a trypo err. I meant `int i = new MyInstatnceClass().getMyNonStaticInt();`. Your answer answers my concern except the first part that draws from the error I made sorry. Thank you for the answer. – Program-Me-Rev Oct 22 '17 at 09:07
  • While the info in this answer is correct, it doesn't actually answer the OP: they want to know if either a static or an instance method call is more performant/ less expensive than the other, not what the types of methods are useful for. The object instantiation in the example isn't part of what they want evaluated; it's just necessary boilerplate to make it syntactically correct. – errantlinguist Oct 22 '17 at 09:28
  • @errantlinguist I disagree with your interpretation of the question.The OP asked to compare performance of two lines of code, and one of them contains object instantiation, so I see no reason to ignore it. – Eran Oct 22 '17 at 09:30
2

Since instance methods can be polymorphically overridden, a very naive JVM implementation must at least initially use a virtual mehod table to find the appropriate method to call. Classes themselves, however are not polymorphic and class methods cannot be overridden. Due to this, they have a simpler lookup mechanism.

However, real-world JVMs are incredibly smart and can tell which methods are never overridden and optimize this lookup away. In other words, in all but the most contrived instances with non-existent JVMs will there be a difference in performance. Instead, use static methods to represent functionalities relevant to the entire class of objects itself rather than to a single instance thereof.

errantlinguist
  • 3,658
  • 4
  • 18
  • 41