Chapter 2 Flashcards
Inner part of class consist of____
- Fields- store date persistently w/i an object
- Constructors- ensures that an object is set up properly when it is first created
- Methods- implement the behavior of an object; provide its functionality
What is another name for fields?
instance variables
Field pattern
- usually start with reserved word: private
- they include a type name (such as int, String, Person, etc.)
- include a user-chosen name for the field variable
- end with semicolon
Fields
- Store values for an object
- Define state of object
- Use inspect to view state
- some values change often
- some change raraely
- always define fields to be private
ie:
private int price;
constructors
- initialize the fields
- have same name as class
- close association w/fields
- store initial values into the fields
- external parameters values for this
For these constructors, why isn’t price set to 0?
{
price = cost;
balance = 0;
total = 0;
}
Because we don’t know what the ticket price will be for this project since it’s entered externally.
Passing data via parameter
- parameters are another sort of variable
- used to hold data
- temporary messenger carrying data originating externally
What is formal parameter vs actual parameter
formal is parameter names such as cost in
TicketMachine(int cost)
while actual is the user supplied value like 500 if user entered that
Assignment
- values are stored into fields & other variables via assignment statements
ie:
variable = expression;
price = cost; - a variable stores a single value, so any previous value is lost
- types must match
Methods
- yellow boxes
- 2 parts: header & body
- always has parenthesis and no semicolons at end
method header
- provides method’s signature
- tells us:
method name
parameters it takes
whether it returns a result
it’s visibility to objects of other classes
Method body
- Encloses the method’s statements with curly brackets
- contain:
declarations- used to create additional, temp variable space
statements- describe actions of the method - a set of declarations and statements b/t curly brackets is a block
Accessor (get) Method
- always has non void return type
- returns value (result) of the type given in header
- contains return statement
T or F: returning is the same as printing
F: returning is not printing!
Returning a value
- some info passed internally b/t 2 different parts of a program
- 1 part requested info from object via method call & return value is the way the object has of passing the info back to caller
Mutator method
- Changes state of object- mutate
- achieved through changing the value of 1 or more fields
typically contain assignment statements
often receive parameters
What kind of method is this code:
Public int getPrice()
{
return price;
}
Accessor
What kind of method is this:
public void insertMoney (int amount)
{
balance = balance + amount;
}
mutator
set mutator methods
- most basic
- fields often have dedicated set mutator methods
- simple distinctive form:
void return type
method name related to field name
single parameter, w/same type as the type of the field
single assignment statement
Local variables
- similar to field variables
- never have “private” or public”
- lifetime is at method execution- created when method is called & destroyed when method finishes
Local variable are defined within a method
- short lived like parameters
- the method sets their values- unlike parameters they do not receive external values
- used for temp calc and storage
- only accessible w/i the method
What is this code doing?
int amountToRefund = balance;
declaring the variable amountToRefund and giving it an initial value
- it is common to initialize local variables W/I their declaration
What is this?
int amountToRefund;
local variable
What is the difference in scope between a local variable and field?
- A scope of a local variable is the block in which its declared while scope of fields is its whole class
- lifetime of LV is time of execution of the block in which its declared while lifetime of field if the lifetime of its containing object
What are the different kinds of variables
fields
parameters
local variables
T or F: Java keywords never contain uppercase letters
True
Field pattern
- usually start with reserved word: private
- they include a type name (such as int, String, Person, etc.)
- include a user-chosen name for the field variable
- end with semicolon
T or F: all fields are automatically initialized to a default value if they are not explicitly initialized
True
so integers are automatically set to 0
Constructor space of the object (or method space for methods)
- Used to provide space to store the values for the constructor’s parameters.
- Created only when constructor executes
What is scope?
- The section of source code from which the variable can be accessed
- scope of parameter is restricted to the body of the constructor/method in which it is declared
- scope of field is the whole class definition so it can be accessed from anywhere in the same class
lifetime
- describes how long the variable continues to exist before it is destroyed
- lifetime of parameter is limited to a single call of a constructor/method
- lifetime of field is the same as lifetime of the object to which it belongs
Effect of mutator
an object will often exhibit slightly different behavior before and after it is called
What compound assignment operator is used when adding an amount to the value in a variable?
+=
balance + = amount;
what is the basic form of a call to println?
System.out.println(something-we-want-to-print);
What are the 3 components in this parameter?
System.out.println(“# “ + price + “ cents.”);
- the string literal: “# “ (note the space after the hash)
- the value of the price field (note that there are no quotes around the field name because we want the field’s value, not its name)
-the string literal: “ cents.” ( note the space character before the word)
_________ “+” is a string-concatenation operator
- when used between a string and anything else
- ie., it concatenate/joins strings together to create a new string
Non-void vs void return type
-non-void: method will return some data to the place it was called from- and that data will be used in the caller for further calcs or program manipulations
-void: return nothing
conditional statement
-Takes 1 of 2 possible actions based upon the result of a test.
-aka if-statements
-general form:
if(perform some test that gives a t/f result) {
Do the statements here if the test gave a true result
}
else {
Do the statements here if the test gave a false result
}
Scope highlighting
colored boxes around some elements, such as methods and if-statements.
T or F: Fields are used to store data that persist throughout the life of a method. As such, they maintain the current state of an object. They have a lifetime that lasts as long as their method lasts.
False: Fields are used to store data that persist throughout the life of an object. As such, they maintain the current state of an object. They have a lifetime that lasts as long as their object lasts.
T or F: Formal parameters have a scope that is limited to their defining constructor or method.
True: Formal parameters have a scope that is limited to their defining constructor or method.
T or F: Fields are defined inside constructors and methods.
False: Fields are defined outside constructors and methods.
T or F: Java requires that every variable be given a type before it can be used.
True: Java requires that every variable be given a type before it can be used.