Instructions
Program listing |
// STATIC VERSUS NON-STATIC TUTORIAL // Copyright (C) 1998-2016 Davin Pearson // Website: http://davin.50webs.com/java-tutorials class FredFlintstone { // properties of the class... public static String name = "Fred Flintstone"; public static String favouriteColour = "blue"; public static int favouriteNumber = 42; // methods of the class... public static void displayMe() { System.out.println("Hello, my name is " + name); System.out.println("my favourite colour is " + favouriteColour); System.out.println("and my favourite number is " + favouriteNumber); } } class StaticTest { public static void main(String[] args) { } } // QUESTIONS: // // (1) Write a BarneyRubble class and a WilmaFlintstone class // with exactly the same properties and methods as the // FredFlintstone class, except that the values of the // properties in the new classes should be different. // // (2) Add some code to the main method to call Fred // Flintstone's displayMe method. // HINT: Remember that to call a normal (non-static) method, // you put the name of the object before the name of the // method. To call a static method, you put put name of the // class in front of the method. // // (3) Add some code to the main method to print out Barney // Rubble's favourite colour WITHOUT using Barney Rubble's // displayMe method. // HINT: To access a static property, put the name of the // class in front of the property. // // (4) Now do the questions from StaticTest2.java.