Objects
Something being created/ manipulated by the program
Characterized by itâs state and behavior
Instance of a class
Object Reference
A variable that represents an object
Class
Encapsulates data
An object is an instance of a class
Public class
Usable by all client programs (pieces of code outside the class)
All classes are public in AP Java subset
Public Methods
Accessible to all client programs
Private Methods/Variables
Can only be accessed by methods in the same class
Static Variable (Class Variable)
Memory allocation only happens once
Shared by ALL INSTANCES of the class
Static Final Variable (constant)
Cannot be changed
Public
Single value that applies to the whole class instead of a new instance for each object of the class
BankAccount.PENALTY for client
PENALTY in own class
Method Header
Public void Withdraw (String password, double amount)
Access Specifier | Return Type | Method Name | Parameter List
Access specifier = who has access
Void = method doesnât return a value
Parameters are separated by commas, require data type
Implementation follows header and is enclosed by {}
Constructor
Method that creates an object of the class
Name is same as class, no return type
No-Argument Constructor
No parameters
Provides initial values for an object
Reference to an Object
Variable assigned values of the object
Constructor with parameters
Sets instance variables to values of those parameters
They can be changed in different references
Accessor Method
Public method that accesses a class object without altering it
.getBalance
Mutator Method
Changes state of an object by modifying at least one instance variable
Instance Methods
Perform operations on individual objects of a class
Constructors, accessors, mutators
Static Methods
Perform an operation for the entire class
Keyword Static in header
Can use a static variable in its code
Driver Class
Main() method is always static
Tests other classes
Method Overloading
2 or more methods in the same class with the same name but different parameter lists
Distinguished by method SIGNATURE
Canât have 2 methods with same signature but different return types, so return type is irrelevant
Scope
Region in which the variable/ method is visible and can be accessed
A classâs instance + static variables and methods belong to the classâs scope (enclosed by {})
DOT OPERATOR NOT NEEDED
Local Variable
Defined inside a method or statement
Scope is from declaration to end of its block
Precedence over instance variables with same name
Block
Piece of code enclosed by {}
This Keyword
Instance methods are called for a particular object, which is an IMPLICIT PARAMETER for the method
Referred to with keyword this
Reference Data Types
Objects and arrays
String, Random, int[], string [] [], Cat, etc
STORES MEMORY ADDRESSES
If object is changed, any references aliased to it will also change
Primitive Data Types
Double, int, char, boolean
STORES VALUE DIRECTLY
Null Reference
Uninitialized object variable
Null pointer
Doesnât refer to an object
Formal Parameters
Placeholders for actual parameters
String acctPassword, Double amount
Actual Parameters
Actual values given to a method call
Match up with formal parameters
Primitive Types as Parameters
Changes made to parameters of method will not affect the values of the arguments in the calling program
Basically goes back to what it was before
Passed by value
Objects as Parameter