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

Chapter 18: Using Methods and Fields from a Java Class

In this chapter:

Fun with word order

import java.util.Scanner;

public class WordOrder {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        
        String string1, string2, string3, string4, string5, string6;
        
        string1 = keyboard.next();
        string2 = keyboard.next();
        string3 = keyboard.next();
        string4 = keyboard.next();
        string5 = keyboard.next();
        string6 = keyboard.next();
        
        System.out.println(string1 + " " + string2 + " " + string3 + " " + string4 + " " + string5 + " " + string6);
        System.out.println(string2 + " " + string1 + " " + string3 + " " + string4 + " " + string5 + " " + string6);
        System.out.println(string2 + " " + string3 + " " + string1 + " " + string4 + " " + string5 + " " + string6);
        System.out.println(string2 + " " + string3 + " " + string4 + " " + string1 + " " + string5 + " " + string6);
        System.out.println(string2 + " " + string3 + " " + string4 + " " + string5 + " " + string1 + " " + string6);
        System.out.println(string2 + " " + string3 + " " + string4 + " " + string5 + " " + string6 + " " + string1);

        keyboard.close();
    }

}
Here's another solution that uses an array of String values:
import java.util.Scanner;

public class WordOrder2 {

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

        String[] strings = new String[6];

        for (int i = 0; i < 6; i++) {
            strings[i] = keyboard.next();
        }

        for (int lineNumber = 0; lineNumber < 6; lineNumber++) {
            for (int i = 0; i < 6; i++) {
                if (i < lineNumber) {
                    System.out.print(strings[i + 1] + " ");
                } else if (i == lineNumber) {
                    System.out.print(strings[0] + " ");
                } else {
                    System.out.print(strings[i] + " ");
                }
            }
            System.out.println();
        }

        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.