Short AnswersTopWarm-up Exercises

Warm-up Exercises

There is nothing to hand in for this part, but make sure you understand these examples.

  1. What is printed by the following program?
    public class Example {
        protected int num;
    
        public Example(int initial) {
            num = initial;
        }
    
        public int getNum() {
            num = num + 1;
            return num;
        }
    
        public static void main(String args[]) {
            Example first = new Example(10);
            Example second = new Example(20);
            System.out.println(first.getNum());
            System.out.println(second.getNum());
        }
    }
    
    What would be printed if we changed the declaration of num to be
        protected static int num;
    
    What does this tell you about static fields?
  2. Browse the javadoc documentation available from the class web page. Look up the structure package's Vector class. Become familiar with how to look up information on these webpages.

Short AnswersTopWarm-up Exercises