• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static method is better to use instead of instance method in terms of memory?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static method are invoked via a class name and instance method are invoked using class instance. For instance method invocation, typically for n number of user , n number of instance is created which is going to occupy more memory but for class method this will not be the case. Even though java automatically manages memory management, instance method still going to occupy memory.
my question is it is always better to use static method instead of instance method in terms of memory?
can any one throw some light on this?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. A method always occupies the same (very small) amount of memory whether it's a static method or an instance method.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krthik s.au wrote:
my question is it is always better to use static method instead of instance method in terms of memory?



you cant go for static method always![infact some situation only you can(if the method is common for all instance)] . please dont confuse yourself with memory management[if instance needs to create,you create it]
 
karthikeyan sai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is given in the JAVA performance Tuning book

"Avoid creating objects in frequently used routines. Because these routines are called
frequently, you will likely be creating objects frequently, and consequently adding heavily
to the overall burden of object cycling. By rewriting such routines to avoid creating objects,
possibly by passing in reusable objects as parameters, you can decrease object cycling."

From the above phrase, it is always to better go with static method rather than creating instance and calling instance method.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krthik s.au wrote:This is given in the JAVA performance Tuning book



Optimization advice often has a short lifespan. When was this book written?
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krthik s.au wrote:
From the above phrase, it is always to better go with static method rather than creating instance and calling instance method.



If you can make a method either static or non-static, then you should make it static. Not for performance reasons but for design reasons. If a method doesn't use information held in individual objects then it should be static because then it uses class-wide information only and then it belongs to the class.

It's generally a big mistake to let optimization considerations influence program design. It's highly likely that you end up with a bad design AND a slow program.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krthik s.au wrote:"Avoid creating objects in frequently used routines. Because these routines are called
frequently, you will likely be creating objects frequently, and consequently adding heavily
to the overall burden of object cycling. By rewriting such routines to avoid creating objects,
possibly by passing in reusable objects as parameters, you can decrease object cycling."

From the above phrase, it is always to better go with static method rather than creating instance and calling instance method.


No, that doesn't follow at all. The quote is talking about creating objects inside methods. It makes no difference at all whether those methods are declared static or not as far as that quote is concerned.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"krthic s,au",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic