Java Reference
In-Depth Information
LISTING 5.1
The Full Text of VolcanoRobot.java
1: class VolcanoRobot extends ScienceRobot {
2:
3: String status;
4: int speed;
5: float temperature;
6: int power;
7: }
This class definition contains four variables. Because these variables are not defined
inside a method, they are instance variables. The variables are as follows:
status —A string indicating the current activity of the robot (for example, “explor-
ing” or “returning home”)
n
speed —An integer that indicates the robot's current rate of travel
n
temperature —A floating-point number that indicates the current temperature of
the robot's environment
n
power —An integer indicating the robot's current battery power
n
Class Variables
As you learned in previous days, class variables apply to a class as a whole, rather than a
particular object of that class.
Class variables are good for sharing information between different objects of the same
class or for keeping track of classwide information among a set of objects.
5
The static keyword is used in the class declaration to declare a class variable, as in the
following example:
static int sum;
static final int maxObjects = 10;
Creating Methods
As you learned on Day 3, “Working with Objects,” methods define an object's behav-
ior—that is, anything that happens when the object is created as well as the various tasks
the object can perform during its lifetime.
This section introduces method definition and how methods work. Tomorrow's lesson
has more detail about more sophisticated things you can do with methods.
 
 
Search WWH ::




Custom Search