Preview

08 - Static Variables and Methods

 1. It is not possible to write your own static variables and methods.

  FALSE

  TRUE

 2. Static variables and methods belong to a class and are called with the Class Name rather than using _____________.

  object variables

  class declarations

  instance fields

  string data types

 3. There is only one copy of a static variable or method for the whole class.

  FALSE

  TRUE

 4. For example, the main method is ______ because there should only be 1 main method.

  dynamic

  static

  public

  private

 5. Static methods can be public or private.

  FALSE

  TRUE

 6. The static keyword is placed right after the public/private modifier and right before the type of variables and methods in their declarations.

  TRUE

  FALSE

 7. To use a static method or variable, you use the ____________ and the dot (.) operator.

  method name

  instance name

  class name

  object data type

 8. Note that static methods only have access to static variables and static methods.
class ClassName {
  // static variable
  public static type variableName;

  // static method
  public static returnType methodName(parameters) {
        // implementation not shown
  }
}
// To call a static method or variable, use the Class Name
System.out.println(ClassName.staticVariable);
ClassName.staticMethod();

  TRUE

  FALSE

 9. Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), but they can access or change the values of static variables.

  FALSE

  TRUE

 10. In the following class Person, there is a static variable called ________________that is incremented each time the Person constructor is called to initialize a new Person object.
  private String phoneNumber;

   // Static counter variable
   public static int personCounter = 0;

   // static method to print out counter
   public static void printPersonCounter() {
        System.out.println("Person counter: " + personCounter);
   }

   // constructor: construct a Person copying in the data into the instance variables
   public Person(String initName, String initEmail, String initPhone)
   {
      name = initName;
      email = initEmail;
      phoneNumber = initPhone;
      personCounter++;
   }

   // toString() method
   public String toString()
   {
     return  name + ": " + email + " " + phoneNumber;
   }

   // main method for testing
   public static void main(String[] args)
   {
      // call the constructor to create a new person
      Person p1 = new Person("Adam", "[email protected]", "123-456-7890");
      Person p2 = new Person("Eve", "[email protected]", "404 899-9955");

      Person.printPersonCounter();
   }
}

  email,name and phoneNumber

  personCounter

  name

  phoneNumber

 11. In the above code, the static method printCounter() prints out its value.

  TRUE

  FALSE

 12. Analyse the code below. The output is Max Temp: 65
public class Temperature
{
   private double temperature;
   public static double maxTemp = 0;

   public Temperature(double t)
   {
        temperature = t;
        if (t > maxTemp)
            maxTemp = t;
   }

   public static void main(String[] args)
   {
        Temperature t1 = new Temperature(75);
        Temperature t2 = new Temperature(100);
        Temperature t3 = new Temperature(65);
        System.out.println("Max Temp: " + Temperature.maxTemp);
   }
 }

  TRUE

  FALSE

 13. Static methods cannot call non-static methods.

  TRUE

  FALSE

 14. Static variables are used with the class name and the dot operator, since they are associated with a class, not objects of a class.

  TRUE

  FALSE

 15. __________ also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

  Class variables

  Private variables

  Class objects

  Method variables