<<< Object Creation | Index | Primitive types and reference types >>> |
The methods are not "runnable" unless some code grabs an object reference and makes a call to accomplish the required task:
Caboose servicecab = new Caboose(); // make another piece of rolling stock freight.add( servicecab ); // add caboose to the train by calling the "add" method
If class contains static fields and methods, such fields and methods do not belong to any objects.
Rather, static fields and methods belong to all objects of the class.
More so, public static fields/methods belong to the entire application -- they become global variables and methods that don't require a particular object instance to run.
A good example of a static method is the main method. main is invoked without any particular object.
<<< Object Creation | Index | Primitive types and reference types >>> |