aspose file tools*
The moose likes Beginning Java and the fly likes Why not only static methods are preferred over instance methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why not only static methods are preferred over instance methods" Watch "Why not only static methods are preferred over instance methods" New topic
Author

Why not only static methods are preferred over instance methods

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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.


Thanks,
Shekar
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Surely you need instance methods in order to call them on multiple objects.


SCJP 1.4, www.gsi3d.org.uk
Kevin de Berk
Greenhorn

Joined: Nov 04, 2003
Posts: 3
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?
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Why not only static methods are preferred over instance methods