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.
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?
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 ]
subject: Why not only static methods are preferred over instance methods