Chapter 4: Creating your own classes!!!


Dice Class

Variables:

Methods:

Every class should have


Creating Your Own Classes


Member Variable Declarations

The syntax is:

<modifiers> <type> <name>;

E.g. private int numSides;

or

<modifiers> <type> <name> = <value>;

E.g. private int numSides = 0;

or

<modifiers> <type> <name> = new <type>(<arguments>);

E.g. private Player scooby = new Player();

Examples of modifiers:


Methods

Modifiers: public, private, static

Return type: void or any variable type, e.g. int, float, Dice, etc

Parameter List:

<method name>(<type> <name>,<type> <name> ,...)


Parameters vs Arguments

The parameter list defines the type of information that can be sent along with a message.

The arguments are the actual values that are sent.

When a message is sent, the value of the argument is placed in the parameter. In above example, the value of n will be 6 in the method setSides.

The number of arguments, their type, and their order must match the parameter list. For example, given a class Animals with a method that calculates the population at a future time given the current populations:

public int calcPopulation(int currentPop, int years, double rate)
{ ...}

then the message could be:

Animals zebras = new Animals();
int pop = zebras.calcPopulation(1012, 2, .034);

But the following are not allowed:

pop = zebras.calcPopulation(.034, 1012, 2);
pop = zebras.calcPopulation(1012, 2);


Flow of Execution


Constructors

Constructors are methods used for creating/initializing new objects.

Recall, the syntax for creating an object is

<object> = new <class>(<arguments>);

The above instruction, sends a message to the constructor method.

Syntax for creating a constructor (compare with normal method):

Compared to a method definition:

Example: A constructor for the Dice class.

public Dice(int n)
{
	numSides = n;

}


Kinds of Variables

Variables differ depending on where they are declared:


Lifetime and Scope


Lifetime and Scope of Variables

Local

Local variables exist only in the block of code in which they are declared. Block is defined by brackets { ... }

{
	int i = 2; 
}
System.out.println("i = " + i);

will give error: variable i undefined because local variables are only accessible from within the block of code where they are declared.

Accessibility: anywhere in that block of code.

Parameters

Parameters exist during execution of method.

public void setSides(int n) 
{
	numSides = n;
}

Accessibility: anywhere in that method.

Member variables


Summary of Scope or Accessibility of Variables

All action takes place inside methods. All methods live within classes or objects. The variables that are accessible depend on where you are:

Inside an instance method of an existing object, the following variables are accessible:

Inside a class method of an existing object, the following variables are accessible:

Thus class variables are accessible from either class or instance methods. But, instance variables are accessible from only instance methods.


Parameter Passing:
"Pass by Value" vs "Pass by Reference"

Pass by Value (primitive types):

When a primitive type is passed as an argument, its value is copied into the parameter. Thus, modifying n above, has no effect on m.

Pass by Reference (objects):

When an object is passed as an argument, only the reference (i.e. address) is copied. Thus both the argument and the parameter point to the same object. Modifying ob above will change outBox as well.


next lecture