// Demonstration of various ways to invoke a method in Java. // Written 2002 by Wayne Pollock, Tampa Florida USA. class InvokeDemo { public static void main ( String [] args ) { InvokeDemo obj = new InvokeDemo(); int i; i = obj.add( 2, 3 ); // i = InvokeDemo.add( 2, 3 ); // Only if "add" declared "static". // i = add( 2, 3 ); // Only if "add" is "static" in current class. obj.aMethod(); } /* static */ int add ( int num1, int num2 ) { return num1 + num2; } void aMethod () // A non-static method. Such methods have a // "current" object. { InvokeDemo obj = new InvokeDemo(); int answer; answer = obj.add( 2, 3 ); // answer = InvokeDemo.add( 2, 3 ); // Only if "add" is "static". answer = add( 2, 3 ); // Only if add is method of curent class. System.out.println( answer ); } }