StaticTest.java Answers

Click here to go back to the questions
// ANSWERS:
//
// (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.
// 

public class BarneyRubble
{
   // properties of the class...
   public static String name            = "Barney Rubble";
   public static String favouriteColour = "red";
   public static int    favouriteNumber = 88;

   // 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);
   }
}

// 
// (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.
// 

FredFlintstone.displayMe();

// 
// (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.
// 

System.out.println(BarneyRubble.favouriteColour);

// 
// (4) Now do the questions from StaticTest2.java.