Lecture 27, CS 302-6, November 4
i. Declare as private
ii. Initialized to ‘zero’ value
1. objects null, ints zero, etc
iii. Emphasize variable scope
i. Accessors
1. ‘getters’
2. Boolean methods
ii. Mutators
1. ‘setters’
iii. Methods can do both as well – this is just a
semantic distinction, not a rule
iv. Implicit variable
1. this
v. Explicit variables
vi. Emphasize variable scope (again)
i. Import
1. find it
2. Note – since we are in the same directory,
don’t need to do this with our classes.
Example, though, is java.util.Scanner;
ii. Declare
1. <Class> <varName>
iii. Construct
1. make it
2. <varName>=new <Class>();
iv. Use
1. use it
<varName>.<method>(<params>)
2. by calling methods from the object
i. Testing parts of a program in isolation,
outside the full program.
ii. aka making sure each class works as desired.
public <className>(<type>
<varName>,...){}
i. No void/return type
ii. Not static
i. Cannot be called like a normal method
[object.constructor() does not work].
i. That's just one example of a constructor that
takes params
i. Overloading refers to having more than one
method with the same name.
ii. This is very common with constructors
1. It is also possible with other methods
iii. If we do this, each one must have unique
arguments passed in.
iv. Unique is based on type, order, and number
1. different variable name but everything else
the same does not count
v. When we new an object, the variables must
match the args for one of the constructors