StaticTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called StaticTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

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.