Overview

Readings Reference ch. 5 · week 5

So far any code could reach into any object and change anything. That’s fine for a ten-line program and a disaster for a big one: if a car’s speed is somehow negative, every line in the program is a suspect. This chapter is about control: deciding who’s allowed to see and change what.

  • Packages control where classes live and which names they can see.
  • Encapsulation (private fields plus methods) controls who can touch an object’s data, so the object can protect itself from nonsense values.
  • Scope controls where a variable’s name means something, and for how long it exists.

It also covers passing arrays to methods, 2D arrays, and reading input with Scanner, all of which come down to the same theme of what code can reach.

Notes

Packages

A package is a named group of related classes, like a folder. It solves two problems that appear as programs grow:

  • Organization. Thousands of classes in one pile would be impossible to navigate. java.util holds utilities, java.io holds input/output, and your labs go in coe318.
  • Name clashes. Two programmers can both write a class called Point. If they’re in different packages, their full names (graphics.Point and geometry.Point) are different, so there’s no conflict. The package name works like a surname.
KindExamples
Built-injava.lang (String, Math; imported automatically), java.util (Scanner, ArrayList)
User-definedcoe318 for your labs

A package is literally a directory: classes in package mypack must be in a folder called mypack. That’s how the compiler finds them.

package coe318;            // first line of the file (NetBeans adds it)
public class Lab3 { int labNum = 3; }

To use a class from another package by its short name, import it. Importing doesn’t copy any code. It just tells the compiler which Lab3 you mean when you write Lab3:

import coe318.Lab3;        // one class (preferred: it's clear where each name comes from)
import coe318.*;           // every class in the package

java.lang is imported automatically, because almost every program needs String and Math.

Example 5.1: Which classes need an import?

A program uses String, Math.sqrt, Scanner and ArrayList. Which of these need an import, and what are the lines?

Show answerHide answer
  • String and Math are in java.lang, which is imported automatically, so they need no import.
  • Scanner is in java.util: import java.util.Scanner;
  • ArrayList is also in java.util: import java.util.ArrayList;

Both can be covered at once by import java.util.*;, but naming each class makes it clear where it comes from.

Encapsulation

Encapsulation means hiding an object’s variables inside the class, so other code can only reach them through the class’s methods. The object becomes a sealed box with a few buttons on the outside.

Why this matters so much:

  1. The object can protect its own rules. A car’s speed should never be negative or above its maximum. If any code can write car.speed = -50, that rule can be broken from anywhere in the program. If the only way to change speed is setSpeed, then setSpeed can check the value, and the rule holds everywhere, guaranteed by one method.
  2. Bugs have one address. If the speed is wrong, only the methods of Car could have set it. You look in one class instead of the whole program.
  3. The inside can change without breaking the outside. Suppose you later store speed in m/s instead of km/h. If other code only ever calls getSpeed(), you update that method to convert, and nothing else in the program needs to change. If other code read speed directly, every one of those lines would now be wrong.

It’s the same reason a car has a pedal instead of exposing the fuel injectors: the driver gets a simple, safe interface, and the engineers stay free to change the engine.

Visibility modifiers

Access modifiers are how Java enforces encapsulation. They’re checked by the compiler, so a violation doesn’t even compile.

ModifierAccessible from
publicAnywhere
privateOnly inside the same class
(none): default / packageAny class in the same package
protectedSame package, plus subclasses (see Ch 9)
class Modifiers {
    public int pubData;
    private int privData;
    private void privMethod() { }
    public void pubMethod() {
        privMethod();          // OK: same class
    }
}
 
Modifiers mod = new Modifiers();   // in another class:
mod.pubData = 1;       // OK
mod.pubMethod();       // OK
mod.privData = 1;      // compile error
mod.privMethod();      // compile error

The restriction is per class, not per object. If main is inside the same class, it can use the private members of any object of that class. private means "only code written inside this class", because that's the code the class's author controls.

Example 5.2: Default vs private access

CarTest is a different class from Car, in the same package. Which lines compile?

// Version 1                          // Version 2
public class Car {                    public class Car {
    String licensePlate;                  private String licensePlate;
    double speed;                         public double speed;
    ...                                   ...
}                                     }
 
// in CarTest.main:
Car miniVan = new Car("ABC", 50, 100);
miniVan.licensePlate = "GHI";
miniVan.speed = 100;
Show answerHide answer
LineVersion 1 (default)Version 2
miniVan.licensePlate = "GHI";OK (same package)Error (private)
miniVan.speed = 100;OKOK (public)

Watch out for miniVan.speed = "100"; (with quotes). A String into a double is a type error no matter what the access is.

Rule of thumb

MemberVisibilityName
Instance variablesprivate—
Methods other classes should callpublicService methods
Helper methods used only inside the classprivateSupport methods

The public methods are the class’s interface, its promise to the rest of the program. Keep it small. Everything private is an implementation detail you can change freely later, and everything public is something other code may depend on forever.

Getters and setters

With private fields, the class decides exactly what to expose:

  • Accessor (getter): returns a field’s value, like getSpeed()
  • Mutator (setter): changes a field, like setSpeed(double)

The point isn’t to write a getter and setter for every field automatically, because that would just be a public field with extra steps. Expose only what other code genuinely needs. A licence plate that should never change gets set once, by the constructor, and has no setter at all:

public class Car {
    private String licensePlate;   // set once, never exposed for change
    private double speed;          // has getter and setter
    private double maxSpeed;       // set once
 
    public Car(String plateNumber, double speedVal, double maxVal) {
        licensePlate = plateNumber;
        speed = speedVal;
        maxSpeed = maxVal;
    }
    public void setSpeed(double speedVal) { speed = speedVal; }
    public double getSpeed() { return speed; }
}

This is where encapsulation pays off. A setter can validate: if (speedVal >= 0 && speedVal <= maxSpeed) speed = speedVal;. A public field can't protect itself like that.

Example 5.3: A setter that protects the object

Car has maxSpeed = 120 and this setter:

public void setSpeed(double speedVal) {
    if (speedVal >= 0 && speedVal <= maxSpeed)
        speed = speedVal;
}

What is speed after setSpeed(80), then setSpeed(150), then setSpeed(-5)?

Show answerHide answer
  • setSpeed(80): , so speed = 80.
  • setSpeed(150): above maxSpeed, so the if fails and speed stays 80.
  • setSpeed(-5): negative, so it also stays 80.

With a public field, car.speed = 150; would have worked and broken the rule. Because the field is private, the setter is the only way in, so the rule can’t be bypassed anywhere in the program.

Example 5.4: Triangle with a tester class

Write Triangle with three private sides, getters, one mutator that sets all three, and getPerimeter(). Then write a tester that reads the sides from the user.

class Triangle {
    private double side1, side2, side3;
 
    public double getSide1() { return side1; }
    public double getSide2() { return side2; }
    public double getSide3() { return side3; }
 
    public void setSides(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
 
    public double getPerimeter() {
        return side1 + side2 + side3;
    }
}
import java.util.Scanner;
 
class TriangleTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Your Side1, Side2 and Side3:");
        double s1 = scan.nextDouble();
        double s2 = scan.nextDouble();
        double s3 = scan.nextDouble();
 
        Triangle tri = new Triangle();
        tri.setSides(s1, s2, s3);
        System.out.println("Sides: " + tri.getSide1() + ", "
                           + tri.getSide2() + ", " + tri.getSide3());
        System.out.println("Perimeter: " + tri.getPerimeter());
    }
}
Show answerHide answer

Input 3 4 5 prints the sides and Perimeter: 12.0.

By convention method names start lowercase, so it's getSide1() and not GetSide1().

Example 5.5: Recursive object chain

public class A {
    private A next;
    private int v;
 
    public A(A n, int i) { v = i; next = n; }
 
    public int getT() {
        int t = v;
        if (next != null)
            t = t + next.getT();
        return t;
    }
}
 
A a = new A(null, 3);
A b = new A(a, 1);
A c = new A(b, 2);
System.out.println(a.getT());
System.out.println(b.getT());
System.out.println(c.getT());
Show answerHide answer

The links make a chain . getT() adds this object’s v to everything further down the chain:

  1. a.getT() = 3 (no next)
  2. b.getT() =
  3. c.getT() =

Figure 5.1: The chain c, b, a, null. Each getT() adds its own v to the total of the objects after it. Figure 5.1: The chain c, b, a, null. Each getT() adds its own v to the total of the objects after it.

Prints 3, 4, 6.

Example 5.6: Fill in the blanks for the chain

Using the same class A, fill in the blanks so the output is 5, 8, 6.

A a = new A(____, ____);
A ___ = new A(____, 1);
A ___ = new A(____, 2);
System.out.println(a.getT());   // 5
System.out.println(b.getT());   // 8
System.out.println(c.getT());   // 6
Show answerHide answer
  1. a is declared first, so it can’t link to anything yet and next = null. For a.getT() = 5, v = 5.
  2. The second object has v = 1. It can link to null or a, and linking to a gives , which matches c. So the second variable is c.
  3. The third has v = 2. Linking to c gives , which matches b.
A a = new A(null, 5);
A c = new A(a, 1);
A b = new A(c, 2);

The variables don't have to be declared in alphabetical order. I work backwards from the totals.

Scope

A variable’s scope is the part of the code where its name means something. It matches where the variable is declared and how long it lives:

VariableScopeVisible…
Instance variableClass scopeAnywhere in the class, from its { to its }. It belongs to the object, so every method can use it.
Local variable / parameterBlock scopeOnly inside the { } block where it’s declared

Why block scope? A local variable only exists while its block is running; it lives on the stack and disappears when the block ends (Ch 3). Letting code outside the block use the name would mean referring to something that doesn’t exist anymore, so the compiler forbids it. A bonus is that small scopes keep names from colliding: a loop counter i in one method has nothing to do with an i in another.

Shadowing. When a local variable or parameter has the same name as an instance variable, the name refers to the nearest declaration, the local one. The field is still there, just hidden (“in its shadow”) inside that method. this.name reaches past the shadow to the field:

public Point(double x, double y) {
    this.x = x;    // field = parameter
    this.y = y;
}

Shadowing in constructors and setters is normal, because the parameter’s job is to carry a value for the field of the same name. Shadowing by accident (a local you forgot shares a field’s name) is a classic bug: the method changes the local and the field silently stays the same.

Example 5.7: Shadowing and this

What prints for each object?

public class Point {
    private double x, y;
 
    public Point() { x = 1; y = 1; }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
        this.x++;
        System.out.println("this.x = " + this.x);
        System.out.println("x = " + x);
    }
 
    public Point(double x, double y, boolean dummy) {
        x++;
        System.out.println("x = " + x);
    }
}
 
Point p1 = new Point();
Point p2 = new Point(2, 3);
Point p3 = new Point(2, 3, false);
Show answerHide answer
  1. p1: there are no parameters, so x and y mean the fields. It sets them to 1.0 and prints nothing.
  2. p2: field x = 2, then this.x++ makes the field 3, while the parameter x is still 2.
    this.x = 3.0
    x = 2.0
    
  3. p3: there’s no this., so x++ bumps the parameter to 3. The fields never get set and stay 0.0.
    x = 3.0
    

Arrays as parameters

Passing an array follows the same rule as any argument (Ch 4): Java copies what’s in the variable. What’s in the variable differs:

PassingWhat gets copiedMethod can change caller’s data?
The whole array, f(xyz)The reference to the arrayYes: the method reaches the same array
One element, f(xyz[3])The element’s value (if it’s a primitive)No: the method gets its own copy of that number

So a method like void zeroOut(int[] a) { a[0] = 0; } really does change the caller’s array, since there’s only one array and both variables point at it. void zero(int n) { n = 0; } called as zero(xyz[3]) changes nothing, because n is just a copy of the number that was in slot 3.

Example 5.8: Arrays and elements as arguments

What does each aProc print?

void aProc() {                     void aProc() {
    int[] xyz = new int[12];           int[] xyz = new int[12];
    theProc(xyz);                      theProc(xyz[3]);
    System.out.println(xyz[3]);        System.out.println(xyz[3]);
}                                  }
void theProc(int[] mno) {          void theProc(int mno) {
    mno[3] = 15;                       mno = 15;
}                                  }
Show answerHide answer
  1. Left: the whole array gets passed, so mno refers to the same array. It prints 15.
  2. Right: xyz[3] is an int, so only its value (0) is copied. It prints 0.

Example 5.9: Fill in fiddle

Fill in the blanks so the program prints the four lines 10,5 · 15,19,12 · 20,21 · 19,15.

public static void main(String[] args) {
    int[] test = new int[2];
    test[0] = 10;
    test[1] = 5;
    System.out.println(test[0] + "," + test[1]);
    fiddle(test, test[1]);
    System.out.println(test[0] + "," + test[1]);
}
static void fiddle(int[] test, int element) {
    ___ = 15;
    ___ = 19;
    ___ = 12;
    System.out.println(___ + "," + ___ + "," + element);
    test = new int[2];
    test[0] = ___;
    test[1] = ___;
    System.out.println(test[0] + "," + test[1]);
}
Show answerHide answer
  1. Line 1 (10,5) is printed by main before the call.
  2. The last line is main’s second print, so after fiddle the original array has to hold 19, 15. Those changes happen before test = new int[2], while test still points at the original.
  3. Line 2 ends with element, so element = 12, and the other two values are 15, 19. That’s test[1] and then test[0].
  4. After test = new int[2], test refers to a new local array. Setting it to 20, 21 gives line 3 without touching main’s array.
static void fiddle(int[] test, int element) {
    test[1] = 15;
    test[0] = 19;
    element = 12;
    System.out.println(test[1] + "," + test[0] + "," + element);   // 15,19,12
    test = new int[2];
    test[0] = 20;
    test[1] = 21;
    System.out.println(test[0] + "," + test[1]);                   // 20,21
}

Figure 5.2: Memory at the end of fiddle. Changes to the original array stay, but test = new int[2] only moved fiddle's own reference. Figure 5.2: Memory at the end of fiddle. Changes to the original array stay, but test = new int[2] only moved fiddle’s own reference.

2D arrays

Java doesn’t really have a grid type. A 2D array is an array of arrays: an outer array whose elements are references to inner arrays, one per row.

int[][] A = { {  1,  0, 12, -1 },
              {  7, -3,  2,  5 },
              { -5, -2,  2,  9 } };
int[][] B = new int[3][4];      // 3 rows, 4 columns, all 0
 
A.length        // 3: number of rows (length of the outer array)
A[0].length     // 4: columns in row 0 (length of that inner array)
A[1][2]         // 2: row 1, then element 2 of that row

Reading A[1][2] from left to right explains it: A[1] is the second row (itself an array), and [2] picks element 2 from it. That’s also why A.length gives rows and A[0].length gives columns. Since each row is its own array, rows could even have different lengths, which is why careful code uses A[row].length rather than assuming every row is the same.

Go through it with nested loops, rows on the outside and columns on the inside:

for (int row = 0; row < A.length; row++)
    for (int col = 0; col < A[row].length; col++)
        System.out.println(A[row][col]);

The inner loop runs completely for each pass of the outer loop, so this visits row 0 left to right, then row 1, and so on, the same order you’d read a page.

Example 5.10: Filling a 2D table

int[][] table = new int[5][10];
for (int row = 0; row < table.length; row++)
    for (int col = 0; col < table[row].length; col++)
        table[row][col] = row * 10 + col;
Show answerHide answer
  1. table.length = 5 rows, table[row].length = 10 columns, so elements
  2. Each element is row * 10 + col, so row 0 has 0–9, row 1 has 10–19, and so on up to row 4 with 40–49
  3. Printing with the same nested loops lists 0 through 49, one per line

User input with Scanner

Scanner (in java.util) reads text from the keyboard or a file and turns it into the values you ask for:

import java.util.Scanner;
 
Scanner scan = new Scanner(System.in);                     // console
Scanner fileScan = new Scanner(new FileReader("myfile"));  // file

How it reads. Everything the user types, including the Enter key (a newline character), goes into a buffer. Scanner works through that buffer from the front. Most methods read one token, a chunk of text up to the next whitespace (space, tab or newline):

MethodReads
nextLine()The whole rest of the current line, spaces included, and consumes the newline
next()One token (up to the next whitespace)
nextInt() / nextDouble()One token, converted to a number
hasNextInt() / hasNextDouble()true if the next token is that type. It only looks, without consuming anything

If the buffer is empty, the program waits at the next...() call until the user types something and presses Enter.

The nextInt() then nextLine() trap. If the user types 42 and presses Enter, the buffer holds 42\n. nextInt() takes the 42 but leaves the \n behind. A nextLine() right after it finds that leftover newline, returns an empty string immediately, and seems to "skip" the input. Fix it by calling an extra scan.nextLine(); after nextInt() to clear the rest of the line.

Example 5.11: The leftover newline

The user types 42, presses Enter, then types Hello World and presses Enter. What do n and s hold?

int n = scan.nextInt();
String s = scan.nextLine();
Show answerHide answer

n is 42, but s is the empty string "". After nextInt() takes 42, the buffer still holds \nHello World\n. nextLine() reads up to the first newline, which is right at the front, so it returns nothing. Hello World is still waiting in the buffer.

Add scan.nextLine(); after nextInt() to throw away the leftover newline, and then s is "Hello World".

nextInt() throws an InputMismatchException if the next token isn't an int (the user typed abc). Because hasNextInt() only peeks, it lets you check first and handle bad input yourself:

while (!scan.hasNextInt()) {
    System.out.println("Please enter a whole number.");
    scan.next();               // throw away the bad token
}
int n = scan.nextInt();

Example 5.12: Scanner with validation

Read a first name and a birth year, then print the age. It shouldn’t crash if the year isn’t a number.

import java.util.Scanner;
 
class Age {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter your first name & year of birth");
        String firstName = scan.next();          // one token
        if (scan.hasNextInt()) {                  // check before reading
            int yob = scan.nextInt();
            System.out.println("Hello " + firstName + ", you are "
                               + (2026 - yob) + " years old!");
        } else {
            System.out.println("Input error!");
        }
    }
}
Show answerHide answer

Input Rocky 2006 prints Hello Rocky, you are 20 years old!, and input Rocky abc prints Input error! instead of throwing an exception.

(2026 - yob) needs the parentheses. Without them, "... are " + 2026 - yob tries to subtract from a String and won't compile.

Key syntax

Encapsulated class template

public class Name {
    private Type field;                          // private data
 
    public Name(Type field) { this.field = field; }
 
    public Type getField() { return field; }     // getter
    public void setField(Type field) {           // setter (only if needed)
        this.field = field;
    }
}

Reading input

Scanner scan = new Scanner(System.in);
String word = scan.next();
int n = scan.nextInt();
double d = scan.nextDouble();
String line = scan.nextLine();

2D array size

Common mistakes

  • Making instance variables public (that breaks encapsulation)
  • Forgetting this. when a parameter shadows a field, so the field never changes
  • Expecting a method that changes one array element passed by value to affect the caller
  • Expecting test = new int[2] inside a method to replace the caller’s array
  • Mixing up A.length (rows) and A[0].length (columns)
  • Forgetting import java.util.Scanner;
  • Calling nextInt() without checking hasNextInt() and crashing on bad input
  • Writing message.Equals("quit") when Java is case-sensitive and it’s equals
  • Naming a class 2DArray, since identifiers can’t start with a digit