Home Unit 5 Classes
Post
Cancel

Unit 5 Classes

Static Variables and Methods

Static Methods

  • Define behaviors of a class (belong to class, NOT object/instance of class)
  • Keyword static in header before method name
  • Can only: access/change static variables
  • Can’t: access/change instance variables or the class’ instance variables, no this reference

Practice

Should we use a static or non-static method?

1
2
3
4
5
6
7
8
9
public class Assignment{
    // next classwork/homework ID is NEXT number of classwork/homework that will be created
    private static int nextClassworkID = 1;
    private static int nextHomeworkID = 1;
    private String name;
    private int pointValue;

    // constructors and methods not shown
}

Question: What is the static data and what is the instance data?

Answer: static is nextClassworkID and nextHomeworkID, instance is name and pointValue

Question: A method getGrade is given an int score earned on the assignment and returns the percentage (as a decimal) earned by that score. Would this be implemented as a static or non-static method?

  • Think: What data does it need access to? If needs access to instance data, needs to be non-static. If only need access to static data, it can be static.

Answer: non-static, since the method would need to access pointValue which is an instance variable.

Popcorn Hacks: write getGrade method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// solution
public class Assignment{
    // next classwork/homework ID is NEXT number of classwork/homework that will be created
    private static int nextClassworkID = 1;
    private static int nextHomeworkID = 1;
    private String name;
    private int pointValue;

    public double getGrade(int score){
        // note the double typecast
        return ((double) score / pointValue);
    }
    // constructors and methods not shown

}

Question: Would a method that reports the total number of assignments be static or non-static?

Answer: static, since the method would only need to access static data

Popcorn Hacks: write this method totalAssign

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// solution
public class Assignment{
    // next classwork/homework ID is NEXT number of classwork/homework that will be created
    private static int nextClassworkID = 1;
    private static int nextHomeworkID = 1;
    private String name;
    private int pointValue;

    public int totalAssign(){
        // subtract two because the IDs are NEXT numbers that will exist, so subtract 1 from both -> -2
        return (nextClassworkID + nextHomeworkID - 2);
    }
        // constructors and methods not shown

}

Multiple Choice Question

1
2
3
4
5
6
7
8
9
10
11
12
public class Example{
    private static int goal = 125;
    private int current;

    public Example (int c) {
        // code segment 1
    }
    public static void resetGoal (int g) {
        // code segment 2
    }
    // other methods not shown
}

Which of the following statements is true?

  1. Code segment 1 can use the variable goal but cannot use the variable current.
  2. Code segment 1 cannot use the variable goal but can use the variable current.
  3. Code segment 2 can use the variable goal but cannot use the variable current.
  4. Code segment 2 cannot use the variable goal but can use the variable current.
  5. Both code segments have access to both variables

Answer: 3

Question: Which ones can code segment 1 (constructor) use? Anything (constructor) can call a static variable, but a static a static method can only call static variable.

Static Variables

  • Define static variables that belong to the class, all object of the class sharing that single static variable (associated with class, NOT objects of class)
  • Either public or private
  • static keyword before variable type
  • used with class name and dot operator

Multiple Choice Question

1
2
3
4
5
6
7
8
9
10
11
12
public class Example{
    private static int goal = 125;
    private int current;

    public Example (int c) {
        // code segment 1
    }
    public static void resetGoal (int g) {
        // code segment 2
    }
    // other methods not shown
}

Which of the following statements is true?

  1. Objects e1 and e2 each have a variable goal and variable current.
  2. Objects e1 and e2 share the variable goal and share the variable current.
  3. Objects e1 and e2 share the variable goal and each have a variable current.
  4. Objects e1 and e2 each have a variable goal and share the variable goal and share the variable current.
  5. The code does not complie because static variables must be public.

Answer: 3

Scope and Access

  • Local variables: variables declared in body of constructors and methods, only use within constructor or method, can’t be declaed public or private
  • If local variable named same as instance variable, within that method the local variable will be referred to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Bowler{
    private int totalPins;
    private int games;
    
    public Bowler(int pins){
        totalPins = pins;
        games = 3;
    }

    public void update (int game1, int game2, int game3) {
        // local variable here is newPins
        int newPins = game1 + game2 + game3;
        totalPins += newPins;
        games += 3;
    }
}

Multiple Choice Question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Example{
    private int current;
    
    public Example(int c){
        double factor = Math.random();
        current = (int)(c * factor);
    }

    public void rest (int num) {
        private double factor = Math.random();
        current += (int)(num * factor)
    }
    
    // other methods not shown
}

Which of the following is the reason this code does not compile?

  1. The reset method cannot declare a variable named factor since a vriable named factor is declared in the constructor.
  2. The reset method cannot declare factor as private since factor is a local variable not an instance variable.
  3. The constructor cannot declare a variable named factor since a variable named factor is declared in the reset method.
  4. The constructor cannot access the isntance variable current since current is private.
  5. There is no syntax error in this code and it would compile.

Answer: B

This post is licensed under CC BY 4.0 by the author.