Chapter 4 - Methods use instance variables Flashcards
Point of an object
Has behavior that acts on it’s state
Methods use
Instance variable values
A method uses
Parameters
A caller passes
Arguments
What do you have to do if a method takes a parameter
You must pass it a value of the appropriate type
Arguments are
The things you pass into methods
A parameter is nothing more than
A local variable with a type and a name that can be used inside the body of the method
An example of an argument
d.bark(3); <— 3
An example of a parameter
void bark (int numOfBarks) <—numOfBarks
What does void mean?
Methods do not return anything back
Can methods have multiple parameters?
Yes. t.takeTwo(12, 34);
Java is?
Pass-by-value
What does pass-by-value mean
Pass-by-copy
Call the go()method, passing the variable x as the argument
foo.go(x);
Value means?
bits inside the variable. A variable is a reference to an object.
A method must
Declare a return type
What is the purpose of a getter
To send back, as a return value, the value of whatever it is that particular Getter is supposed to be Getting
What is the purpose of a setter
To take an argument value and use it to SET the value of an instance variable
How do you hide data
With the public and private access modifiers
An encapsulation starter rule of thumb
Mark your instance variables PRIVATE
Provide PUBLIC getters and setters for access control
Declare and create a Dog array to hold 7 Dog references
Dog [] pets;
pets = new Dog [7];
Instance variables always get a
Default value. Booleans -- Flase References -- Null Ints -- 0 Floats -- 0.0
Why don’t you have to initialize instance variables
Because they always have a default value
What is the default value for a reference variable
null
The difference between instance and local vairables
- Instance variables are declared inside a class but not within a method
- Local variables are declared within a method
- Local variables MUST be initialized before use
Do local variables get a default value
No.
What do you need to determine if two objects are equal
The .equals()method
The == is used only to compare?
The Bits in two variables
When do you use ==
To compare two primitives, or to see if two references refer to the same object
When do you use the equals method
To see if two different objects are equal
A method uses …..
Parameters
A caller passes
Arguments