2.1-2.2
2.1 Objects: Instances of Classes
A class serves as a blueprint for creating objects.
Determines the type of attributes and behaviors for objects.
Example: Car class may include attributes such as:
yearbodyColorgasAmount
Behaviors may include:
drivefillTank
Each object has its own values for attributes.
Example: One Car object may have a
yearof 2015, another could have ayearof 2019.
Objects have shared behaviors as defined by the class.
Analogy: A class is like a cookie cutter, creating multiple objects (cookies).
Important pre-existing classes in Java for AP CS A exam include:
StringMathIntegerDoubleArrayListObject
Aspects of a Class
Attributes/Fields: Data elements for objects are known as fields or instance variables.
Constructors: Procedures for creating an object. These set initial attributes when the object is created.
Methods: Behaviors of an object defined within the class.
2.2 Creating and Storing Objects (Instantiation)
Instantiation: The process of creating objects using the keyword
newfollowed by a constructor call.Example syntax:
Dog d1 = new Dog("Rover", 7);
Parameters in Constructors:
Allow initial values to be passed, establishing the object's initial state.
Reference Variable Declaration
Simply declaring a reference variable does not create the object:
Example:
Dog d1;// only declares d1
The variable at this point holds
null, indicating no association with an object.
Creating the Object
To instantiate the Dog object:
d1 = new Dog("Rover", 7);// Now d1 holds the memory address of the Dog object.
Using Existing Classes:
Example of creating a Scanner object:
Scanner s = new Scanner(System.in);// Requires importing the Scanner class.
Constructors
A class can have 0, 1, or multiple constructors.
Must have the same name as the class.
No return type.
Constructors initialize field values during object creation.
Constructor Overloading: A class may have several constructors with different numbers/types of parameters.
If no constructor is defined, a default constructor (no parameters) is automatically invoked, setting fields to default values (0 or null).
Signature of a Constructor
The signature consists of the constructor name and parameter list.
The parameter list specifies:
Types of values passed to the constructor.
Names of the variables used.
Formal Parameters: The variable names in the constructor.
Actual Parameters: Data passed to the constructor from a client program.
Parameter Compatibility
Actual parameters must be compatible with formal parameters in type.
Variable names in the actual parameters do not need to match the names of formal parameters, but their data types must align.
Call By Value and Parameter Types
Java uses Call By Value to pass parameters to constructors and methods.
Contrast with Call By Reference used in some other languages.
Call By Value:
Formal parameters are initialized with copies of the actual parameters.
Primitive Types vs. Reference Types:
Primitive Types (e.g., int, double): A copy of the actual value is passed.
Reference Types (e.g., Dog): A copy of the reference (memory address) is passed.