CS101 Study Guide

Unit 6: User-Defined Methods

6a. Write methods with zero or more parameters

  • What is the difference between a class parameter and variable?
  • What are the two ways to change an attribute owned by a particular object?
  • What is the lifetime of a variable?
  • Is it valid to specify a class without specifying a constructor and destructor? How so?

In earlier readings, we saw that a class is composed of behaviors and attributes. These are used to describe a class of objects (all trucks in general, for example). Another name for "behavior" is method, and for "attribute", parameter. Depending on which author you are reading, you will see those synonyms regarding class descriptions. The figure illustrates this idea.

In Unit 6, we learn how to write class methods, how to pass parameters (arguments) into a class method, and how to retrieve data from a class. Recall that an object is a specific instance of a class. There can be many trucks, for example. All objects of a class operate (behave) in exactly the same way. But, each object may contain different values for its parameters (attributes). Each object is independent of all other objects, even those of the same class. 

Independence has interesting implications for concurrent processing, whether that takes place on the same physical machine in the same program memory (threads, parallel processing), or on the same physical machine in different program memory (distributed processing), or on different machines connected to a geographically-dispersed network (distributed processing). All three types can be said to be concurrent in that they have more than once process executing at the same time. Beware of vendors who claim "distributed processing" when they are actually spawning processes only in different program memory on the same physical machine, in the same physical memory of that machine, albeit in different program memory. Distributed processing is highly limited in its impact, and it imposes significant operating system overhead, if it is exercised within only one machine's physical memory.

With that in mind, we get into the concept of "scope". Scope refers to the accessibility and lifetime of variables and class parameters. The value of a class parameter is only valid for a particular instance of a class. It persists for as long as the object exists. A variable only exists only within its scope. Scope is generally marked by curly brackets { <variable specification> }. By "accessibility" we mean that a parameter or variable cannot be accessed outside its scope. An exception is that a class parameter or method can be accessed outside the class if the parameter or method is specified as "public". Except for constructor, destructor, mutator, and accessor methods, making attributes and behaviors public is generally a bad programming and design practice. But, there are cases when practicality makes it necessary.

Another important fact is that Java is a pass-by-value language. In other words, if you pass a variable's value into a method, it is only the value that arrives, not a reference to the variable. Even if you use the same variable name inside the method, changes to the value will not be reflected outside the method. One consequence of that fact is that if you pass an object into a method, it is not possible to change the object itself. However, it is possible to change public parameters of that object and to call its public methods. Those new values persist even once the originally-called method ends. That is because you are referring to a specific object and that object contains its own methods and parameters.

Review: 


6b. Make method calls

  • How is it possible to have two behaviors with exactly the same name within a class description?
  • When can there be a class attribute with the same name as a method variable?
  • Is it possible to change the value of a variable that is passed into a method so that its value outside the method is changed?
  • Can there be two variables with the same name within the same scope?

Methods have signatures. If the signature of the method is different, the method name can be the same. A method's signature consists of its name, the number of input parameters (arguments), and the types of its parameters. If any of those are different, then the signature is different and the correct method will be called by the JVM. There is no violation of scope in such cases.

Scope itself speaks to the existence of a variable. It exists only within its scope. Curly brackets,
{ < code > }, denote scope. For example:

variableName within //Scope A

variableName within // Scope B

{

// Scope A

int variableName = 0;

{

variableName -= 1; // valid access

}

}

{

{

// Scope B

int variableName = 0;

}

variableName -= 1; // invalid access

}

Be aware of the this pointer. this refers to the object at hand. For example: A class may have an attribute named thisAttribute. It may require initialization. A parameter of the class constructor may have the name thisAttribute. How does the program tell them apart? It is certainly valid to say, thisAttribute = thisAttribute, or to ask, thisAttribute == thisAttribute, within the constructor. But, that only refers to the constructor's input parameter. To initialize the class attribute, it is necessary to write,  this.thisAttribute = thisAttribute. this references the object and .thisAttribute refers to the attribute in question. By itself, thisAttribute refers only to the method's input parameter or to some other variable defined within the method.

Review Parameters, Local Variables, and Overloading: Sections 2, 3, 5, 16, 17, 20; however, the other sections provide good examples and further explanation.


Unit 6 Vocabulary

This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.

  • accessibility 
  • arguments
  • attributes
  • concurrent 
  • concurrent processing
  • distributed processing 
  • method
  • method signatures
  • parallel processing
  • parameter
  • pass-by-value language
  • public
  • scope
  • threads
  • variable