• Post Reply Bookmark Topic Watch Topic
  • New Topic

static method or static variable

 
Rohit Chatterjee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

Which one of the 2 approaches will be more effecient

How does the performance impact
Using static variable instead of static method

Or

is it same?




// using static method

If ( Logger.isEnabled() ) {

// do something

}



// using static variable

If ( Logger.isEnabled ) {

// do something

}

Kindly respond

Rgrds
Rohit
 
Cedric Crowe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Chatterjee:
Hi All

Which one of the 2 approaches will be more effecient

How does the performance impact
Using static variable instead of static method

Or

is it same?




// using static method

If ( Logger.isEnabled() ) {

// do something

}



// using static variable

If ( Logger.isEnabled ) {

// do something

}


Rgrds
Rohit

Using a variable would be *slightly* faster. But the difference will probably be so small you won't notice any difference. Newer JDK's will probably inline the method call anyway eliminating the difference. Exposing a field publicly is generally a very bad idea. Rather use the method and let HotSpot do the optimizing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!