Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Why not only static methods are preferred over instance methods

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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 ]
 
I brought this back from the farm where they grow the tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic