Language:EN
Pages: 2
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
public int die number showing the second die pairo

Public int die number showing the second die pairofdice constructor

CHAPTER 5. OBJECTS AND CLASSES 219

this section. Suppose that we want a Drawable object that draws a filled, red, 100-pixel square. Rather than defining a new, separate class and then using that class to create the object, we can use an anonymous class to create the object in one statement:

Classes, as I’ve said, have two very distinct purposes. A class can be used to group together a set of static member variables and static methods. Or it can be used as a factory for making objects. The non-static variables and methods in the class definition specify the instance variables and methods of the objects. In most cases, a class performs one or the other of these roles, not both.

Sometimes, however, static and non-static members are mixed in a single class. In this case, the class plays a dual role. Sometimes, these roles are completely separate. But it is also possible for the static and non-static parts of a class to interact. This happens when instance methods use static member variables or call static member subroutines. An instance method belongs to an object, not to the class itself, and there can be many objects with their own versions of the instance method. But there is only one copy of a static member variable. So, effectively, we have many objects sharing that one variable.

public int die1; // Number showing on the first die. public int die2; // Number showing on the second die.

public PairOfDice() {
// Constructor. Creates a pair of dice that // initially shows random values.

}

die1 = randGen.nextInt(6) + 1; // Use the static variable!

private String name; // Student’s name.

private int ID; // Unique ID number for this student.

public String getName() {
// Accessor method for reading the value of the private // instance variable, name.

return name;
}

} // end of class Student

Since nextUniqueID is a static variable, the initialization “nextUniqueID = 0” is done only once, when the class is first loaded. Whenever a Student object is constructed and the constructor says “nextUniqueID++;”, it’s always the same static member variable that is being incremented. When the very first Student object is created, nextUniqueID becomes 1. When the second object is created, nextUniqueID becomes 2. After the third object, it becomes 3. And so on. The constructor stores the new value of nextUniqueID in the ID variable of the object that is being created. Of course, ID is an instance variable, so every object has its own

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Tammy Pearson

PageId: ELI52ED5DD