Workshop: Using Class Methods and Variables In Java


Workshop: Using Class Methods and Variables
At the insistence of every attorney and management executive in the Macmillan family of computer publishers, the workshop for this hour will not be the creation of a working virus program. Instead, you'll create a simple Virus object that can do only one thing: Count the number of Virus objects that a program has created and report the total.
Load your word processor and create a new file called Virus.java. Enter Listing 11.1 into the word processor and save the file when you're done.
Listing 11.1. The full text of Virus.java.

 1: public class Virus {
 2:     static int virusCount = 0;
 3:
 4:     public Virus() {
 5:         virusCount++;
 6:     }
 7:
 8:     static int getVirusCount() {
 9:         return virusCount;
10:     }
11: }

Compile the file, and then return to your word processor. You need to create a short program that will create Virus objects and ask the Virus class to count them. Open up a new file and enter Listing 11.2. Save the file as VirusLook.java when you're done.
Listing 11.2. The full text of VirusLook.java.

1: class VirusLook {
2:     public static void main(String arguments[]) {
3:         Virus smash = new Virus();
4:         Virus crash = new Virus();
5:         Virus crumble = new Virus();
6:         System.out.println("There are " + Virus.getVirusCount() + "  7:         viruses.");
8:     }
9: }
Compile the VirusLook.java file, and then run it with the java interpreter by typing the following command:
java VirusLook
The output should be the following:
There are 3 viruses.
Summary
You now have completed two of the three hours devoted to object-oriented concepts in this book. You've learned how to create an object and give behavior and attributes to the object and to its own class of objects. Thinking in terms of objects is one of the tougher challenges of the Java programming language. Once you start to understand it, however, you realize that the entire language makes use of objects and classes.
During the next hour, you'll learn how to give your objects parents and children.
Q&A
Q Can constructor methods send back a value like other methods?

A
No, because there's no way to receive that value. Unlike other methods that can be used as part of an equation, the argument of a method, or other statements, constructors are only handled in response to a new statement. There's no way for that statement to receive a value that would be sent by the method.

Q Do you have to create an object to use class variables or methods?

A
Because class variables and methods aren't associated with a specific object, you don't
need to create an object solely for the purpose of using them. The use of the
Integer.parseInt() method is an example of this because you don't have to create a new Integer object just to convert a string to an int value.

Q What's the difference between the Integer object and the int variable type?

A
The first is an object, and the second is a simple variable type. Each of the variable types such as
char, int, and float has a corresponding object. The object is used when you want to use an object's methods or treat the variable like an object. Because an Integer object can do things in a program that the int variable type cannot, it is convenient to have both.
Quiz
The following questions will test whether you have the attributes and behavior to understand object-oriented programming techniques.
Questions
1. What is a method an example of in a Java class?

(a) attributes
(b) statements
(c) behavior

2.
If you want to make a variable a class variable, what statement must you use when it is created?

(a)
new
(b)
public
(c)
static

3.
What is the name for the part of a program in which a variable lives?

(a) its nest
(b) the scope
(c) variable valley
Answers
1. c. A method is made up of statements, but it's an example of behavior.

2.
c.

3.
b.

Post a Comment

Previous Post Next Post