/* * Lab #3 * * CS 112 * * A simple program to understand the difference between * a static and non-static method. * */ public class testClass { public static void main( String[] args ) { System.out.println( "Understanding static vs. instance methods!" ); // Test Case 1 // Calling a static method. This is fine. // method2(); // Test Case 2 // Calling a non static method without an instance (i.e. object) // of the class. This will cause a compiler error. // method1(); // Test Case 3 // Properly calling an instance method on an object of the class. // testClass obj = new testClass(); // obj.method1(); } // This is an instance method (non static) and can only be called on an object. public void method1() { System.out.println( "This is an instance method!" ); } // This is a static method and can be called independently public static void method2() { System.out.println( "This is a static method!" ); } }