Variables:
- number of sides
- value of last roll
Methods:
- constructor (initializes variables when the object is first created)
- set number of sides (set method)
- return number of sides (accessor method)
- return the value of last roll (accessor method)
- roll and return value of roll (also sets the value of "last roll" variable)
- print current state of the object (toString method)
Every class should have
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();
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> ,...)
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);
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;
}
Variables differ depending on where they are declared:
Lifetime: Imagine taking a snapshot of memory during the execution of a program. Some variables will not have been created yet, some will already have been garbage collected, and some will be present in memory. The lifetime of a variable refers to the period of execution when that variable is in memory.
Scope: Classes encapsulate variables/methods. Methods encapsulate sets of instructions. A member variable in one class is not directly accessible in another another class. The scope of a variable refers to the region of code where that variable is accessible or recognizable. For example, if you were to insert anywhere in the code the instruction to print out all accessible variables:
System.out.println("The variables are " + a + b + ...);
what variables could be included in the list?
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 exist during execution of method.
public void setSides(int n) { numSides = n; }
Accessibility: anywhere in that method.
- instance variables - exist for lifetime of specific object
Accessibility: any instance method in the class.
Note: if the variable is declared as public, then that variable can be accessed through the object. For example, if numsides is declared public then in main one could access numsides in the object called myDie by writing myDie.numSides. However, member variables should typically be declared private to prevent this type of access. This applies to class variables as well.
- class variables (static) - exist for entire program
- If we wanted all dice objects to have the same number of sides, then we could make numSides a class variable. In this case, numSides would exist even if there was no Dice object.
public class Dice { private static numSides = 6; // class variable .... }
Accessibility: any method (class or instance) in the class. Also, see note above under instance 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.
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.