• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Why not only static methods are preferred over instance methods

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I have a doubt regarding the static and instance methods. A class has static and instance methods. And the difference is that since object creation is expensive we can use static methods .
Now why can't we make all the methods static and why do we need instance methods if et all? Why not make all the methods static? And just like the properties for an object are different for each object are the instance methods different for each object or are they located at one place in the memory which would be accessed by different objects.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely you need instance methods in order to call them on multiple objects.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a static method is a class method, it would have effect on every instance of a class.
Let's say we are making a cardgame, we could make a card-class, if we then want to turn, or remove a card, and use a static class for that, ALL the instances of the class(all the cards) would turn or remove, and we don't want that to happen do we?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the difference is that since object creation is expensive we can use static methods .
The difference is much bigger then that. In object oriented programming you use non-static methods, sometimes called object methods or instance methods. Static methods, wich could also be named class methods, are usefull in some (rare)situations. They're a special case. Most of the time you want to use the non-static versions.
If you would use static methods and fields only, you would end up programming procedural like you would do with C or Pascal, loosing all the benefits of oop. If that's what you want, static methods are the way to do it in Java.
[ April 13, 2004: Message edited by: Tobias Hess ]
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic