Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 6th Edition
by Barry Burd

Chapter 14: Using Methods and Fields from a Java Class

In this chapter:

Plus-size prints

out.println();
out.println("The total for " + fullName + " is " + total + ".");

keyboard.close();

Fill in the blanks

import java.util.Scanner;

import static java.lang.System.out;

public class FillInTheBlanks {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);

        out.print("First adjective: ");
        String adjective1 = keyboard.nextLine();
        out.print("Second adjective: ");
        String adjective2 = keyboard.nextLine();
        out.print("Plural noun: ");
        String noun = keyboard.nextLine();
        out.print("Verb: ");
        String verb = keyboard.nextLine();
        out.print("Adverb: ");
        String adverb = keyboard.nextLine();

        out.println(adjective1 + " " + adjective2 + " " + noun + " " + verb + " " + adverb + ".");

        keyboard.close();
    }
}

Fill in more blanks

import java.util.Scanner;

import static java.lang.System.out;

public class FillInMoreBlanks {

    public static void main(String[] args) {
        var keyboard = new Scanner(System.in);

        out.print("Plural noun: ");
        String noun1 = keyboard.nextLine();
        out.print("Verb: ");
        String verb1 = keyboard.nextLine();
        out.print("Another verb: ");
        String verb2 = keyboard.nextLine();
        out.print("Another plural noun: ");
        String noun2 = keyboard.nextLine();

        out.println("All " + noun1 + verb1 + " when you " + verb2 + " " + noun2 + ".");

        keyboard.close();
    }
}

Show a frame

The code calls three methods belonging to the frame object, namely:
frame.setSize
frame.setTitle
frame.setVisible
When you run the code, you see this frame:

More money

The code calls four static methods, namely: In addition, the code contains the declaration for the static main method.

Books For Dummies

The code contains two instances of the Book class. The javaForDummies variable refers to one instance, and the dosForDummies variable refers to the other instance. So the code contains two copies of the author field -- one copy of the field for each instance of the Book class. The code contains only one copy of the publisher field because the publisher field is static in the Book class.

If you added the following statements to the main method:

Book.publisher = "Wiley Publishing, Inc.";
Book.publisher = "A publisher in the United States";
there would still be only one copy of the publisher field in the code. When Java executed the first added statement, the value of that publisher field would become "Wiley Publishing, Inc.". When Java executed the second added statement, the value of that same publisher field would become "A publisher in the United States".

Use static and non-static fields

When you run this code, you see the following output:
79
443
2
2
In the IntegerHolder class, the value field isn't static. So each of the two IntegerHolder instances (holder1 and holder2) has its own copy of the value variable. So when you set holder1.value to 79, this has no effect on holder2.value. And when you set holder2.value to 443, this has no effect on holder1.value.

But in the IntegerHolder class, the howMany field is static. So there's only one copy of the howMany field, no matter how many instances there are of the IntegerHolder class. So when you add 1 to IntegerHolder.howMany you add 1 to the one and only copy of the IntegerHolder class's howMany variable.

In Java, when holder1 is an instance of the IntegerHolder class, you're allowed to write holder1.howMany. The same is true for any instance of the IntegerHolder class (holder2 for example). This is misleading because the howMany field doesn't belong to the holder1 or holder2 instances of the class. If you ask for the value of holder1.howMany and then of holder2.howMany, you get the same answer because there's only one copy of the howMany field for the entire IntegerHolder class.

Something's wrong here

There's only one copy of the main method for the entire Main class, because the main method is declared to be static. But there's a copy of the keyboard field for each instance of the Main class. Inside the main method, Java doesn't know which instance of the keyboard field to use. So Java can't compile the code.

You can fix the problem by moving the declaration of the keyboard variable inside of the main method. Then the keyboard variable belongs to the one and only main method.

Another way to fix the problem is to make the keyboard field be static:

import java.util.Scanner;

public class Main {
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        int numberOfCats = keyboard.nextInt();
        System.out.println(numberOfCats);
    }
}
If you do, then there's only one copy of the keyboard field for the entire Main class. So, inside the main method, Java knows which copy of the keyboard field you're referring to. So the code compiles and runs.

Just the facts

In the Facts class, all four fields are static.