CS 99

Summer 2001                                                                                       7.25

 

 


Lecture Notes 8

 

Reading 5.2, 5.3, 5.5 Savitch

 

Static Modifier

·         When we declare members inside of a class, we usually give them modifiers.

·         For instance, we can make the members of a class, public or private, final, static, etc.

·         What does the static modifier mean?

·         When we declare a variable without the static modifier, it means that it's an instance variable: each object of the class, when instantiated, has its own copy.

·         But in certain cases we only want one copy of a particular variable to be shared by all the objects of the class. 

·         Example: Suppose in a particular application the constant exp(1) was used often.  In Java, we can use the Math class to get the value and assign it to a constant called E:

final double E = Math.exp( 1.0 );

·         However, if that's the way it was declared in the class, then it would mean every single object of the class would have its own copy of constant E.  What a waste of resources!  Why should each object need to set aside memory space for a number that's never going to change and is the same for each object?

·         Fortunately, Java provides a mean to do this more efficiently.  We declare the variable this way, instead of the way above:

final static double E = Math.exp( 1.0 );

·         The static modifier means that the variable E has "class-wide" scope.  When the program is compiled, one and only one copy of variable E is saved in memory. 

·         Each object of the class knows about the variable and can access it, but the individual objects themselves do not carry their own copies of the value of variable E.

·         We can use the static modifier for methods as well.  When a method is declared static it means that it may be invoked without reference to a particular object.  We usually call a static method using the dot notation with the class name, not an object reference, like so:

Math.random()
Math.ceil( double )


·         Notice in using these methods we invoke them through the class name (Math) and not through a Math object.

·         Class methods affect the class as a whole and not a particular instance of the class. 

·         static methods may only refer to other static members: this has to be true because a static method only knows about those items that belong to the class as a whole, they don't know anything about individual objects and their states

 

public vs. private

·         The member access modifiers public and private are used to control access to a class's instance variables and methods (and static ones too)

·         The primary purpose of public methods is to present to the class's clients a view of the services the class provides, which is to say, the public interface of the class.  Clients of the class need not be concerned with how the class accomplishes its tasks, only with how to ask it to do them.

·         Almost always, the instance variables of a class are made private.  It's usually never a good idea to make instance variables accessible to clients, because there are no guarantees that a client will assign the variables proper values.

·         For instance, let's try to write a simple Bicycle class. 

·         What kinds of things does a bicycle have? A simple first go at it would be to say that a bicycle has a speed, a cadence, and a current gear.  These are our private variables.

·         What kinds of things can we ask a bicycle to do?  We can ask the bicycle to brake, to change cadence, or to change gears.  These are our public methods.

·         Suppose we create a bicycle object that has a speed of 10mph, a cadence of 90 rpm, and is in 5th gear.  Graphically, we can represent that bicycle object like so:


 


·         The diagram has the object's variables in the center, or nucleus of the circle, to demonstrate that the variables are private, and not immediately accessible from the outside world.

·         The public methods brake(), changeGears(), and changeCadence(), surround and hide the object's nucleus from other objects in the program.

·         This conceptual picture of an object as a nucleus of variables packaged within a protective membrane of methods is the ideal that designers of object-oriented systems strive for.

·         However, for practical reasons, an object may wish to expose some of its variables or hide some of its methods.

 

Setter & Getter Methods

·         But if the variables are private, how do we ever get to know what their values are and how do we ever get to change them? 

·         Classes often provide public "get" methods to allow clients to read the values of private data; to enable clients to modify private data, they often provide public"set" methods.

·         These methods may not be called set or get, but often are.  For instance a method that sets the value of private instance variable interestRate would typically be called setInterestRate (double r) and a method that gets its value would typically be called getInterestRate().

·         It seems providing public set and get capabilities is essentially the same as making the instance variables public.  Is that true?  No. 
Public get methods control the formatting and display of the private data.  This can be useful in a number of situation.  Public set methods, on the other hand, can carefully scrutinize attempts to modify their instance variable's values, to ensure that any new values they get will be appropriate.  For instance, an attempt to set the day of the month to 37 would be rejected, or an attempt to set a person's weight to a negative value would be rejected, and so on.

·         Here's an example:

public void setWeight( int pounds ) {
    if ( pounds >= 0 )
        currWeight == pounds;
    else
        System.out.println("Error: Attempt to set weight to illegal value!");
}

Encapsulation

·         Packaging an object's variables within the protective custody of its methods is called encapsulation.

·         Encapsulating related variables and methods into a neat software bundle is a simple yet powerful idea that provides two primary benefits to software developers:

*Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Also, an object can be easily passed around in the system. You can give your bicycle to someone else, and it will still work.

 

*Information hiding: An object has a public interface that other objects can use to

communicate with it. The object can maintain private information and methods that can

be changed at any time without affecting the other objects that depend on it. You don't need to

understand the gear mechanism on your bike to use it.