what is the difference between static and non static methods? :roll:
Joined: Sep 29, 2000
Static methods are class related. You do not need an actual object of the class to invoke them. Notice that all of the methods of the class Math are static. That way you do not need to make a little Math object just to use the method of the class.
Joined: May 10, 2001
Khurram The basic difference is that static methods belong to the class while non-static ones belong to individual objects. The best example of static methods is in the Math class the methods in Math don't need a Math object to work, you just call the method and supply the arguments and the method does it job, there is no instance of Math passed to the method (no 'this'). Static methods normally work with static variables of the class. On the other hand non-static methods need an instance of that class to work on. An example would be a class that keeps track of the number of instances in existence:
In the PublicLibrary consructor every time a library is created we can increment the numberOfLibraries variable. And at any time we can call PublicLibrary.numberOfLibraries(); to get the number of libraries created. The numberOfLibraries is independent of any specific instance of PublicLibrary. The getNumberOfBooks method on the other hand is not a static method so it needs a current instance of the class to act on. The 'this' in that method is not really needed, I put it there to show that the non-static methods actually have an instance of the class to work with, as opposed to the static methods that don't need one. hope that helps, if not let us know what you're still confused on...