Creating and Initializing Objects
Introduction: 2.2.0
- Java class defines:
- What a class knows (
attributes
) - What a class can do (
behaviors
)
- What a class knows (
-
Class has
constructors
which initialize attributes in an object - new object created with
new
keyword followed by class namenew Class()
- Creates a new object of the class, calls constructor
- Constructor has same name as class
new World()
constructs a newWorld
object
- Constructor has same name as class
import frc.robot.RobotMap;
// to create a new object and call the constructor:
// ClassName varName = new ClassName(params);
// creating a world object and calling its constructor
World habitat = new World();
// some code I might use in robotics to create a new motor object,
// with the CAN ID (Controller Area Network ID) of that motor as a parameter
TalonFX intakeMotor = new TalonFX(RobotMap.INTAKE_MOTOR_CAN_ID);
Overloading Constructors: 2.2.1
- Can be more than one
constructor
defined in a class- Known as
overloading
the constructor - Usually includes a constructor without parameters
no argument constructor
- Sets attributes to their default values
- Other constructors which take
parameters
Turtle(habitat)
- a
parameter
is a value which is passed into a constructor- used in the initialization of the object
- Known as
The World Class Constructors: 2.2.2
no argument constructor
creates a window of 640x480World(int width, int height)
initializesWorld
with custom width and heightnew World(300, 400)
creates a world of 300x400
- World coordinates go from the top left corner.
- x increases to the right
- y increases to the bottom
The Turtle Class Constructors: 2.2.3
- Has multiple constructors, all of which require a
world
- Default location for the turtle is the middle of the world
Turtle turguggins = new Turtle(world1);
- Also a constructor which places a turtle at a specific (x,y)
Turtle turguggins = new Turtle(50, 100, world1);
- The order of parameters matter
- The order for turtle must be
(x, y, world)
- The order must be as expected, or data types might not match
- called arguments when implemented
- The order for turtle must be
Object Variables and References: 2.2.4
- You can declare an
object variable
and initialize it tonull
Turtle turguggins = null;
- Creates variable
turguggins
null
means it doesn’t refer to anything yet- can create the object later
turguggins = new Turtle(world1);
- can also just declare and initialize in the same line
Turtle turguggins = new Turtle(world1);
- Creates variable
- An object variable holds a reference to an object
- A
reference
is a way to find an object in memory - Like a tracking number on a package
- A
Video
- represent null as an X
Dog fido = new Dog(); // Dog fido gets a reference to a new dog
// we make a new variable which can reference a dog
Dog dog = null; // is the same as...
Dog dog1;
World world1 = new World();
// create and init on seperate lines
Turtle turguggins = null;
turguggins = new Turtle(world1);
// create and init on one line
Turtle turguguggins = new Turtle(world1);
Constructor Signatures: 2.2.5
- when using a
library
someone else created, you can look up how to use the code using documentation- Documentation for turtle class
- lists the
signatures
(headers) for the constructors and methods- tells you name, and
parameter list
for constructors and methodsparameter list
lists the variables and datatypes used in a given method
- tells you name, and
- Constructors are
overloaded
when multiple exist- Both constructors must have different signatures
- not the same number, type, and/or order of parameters
- Both constructors must have different signatures
public class Date
{
/* ATTRIBUTES */
private int year;
private int month;
private int day;
/* CONSTRUCTORS */
/* A constructor to initialize to today's date */
public Date() {
// implementation not shown
}
/* a constructor to initialize the attributes for a date with given parameters */
public Date(int year, int month, int day) {
// implementation not shown
}
}
Formal and Actual Parameters: 2.2.6
Formal parameters
are what is declared in the method- in
Date(year, month, day)
,year
,month
, andday
are formal parameters
- in
Actual parameters
are argumentsnew Date(2021, 11, 1)
- Date is
call by value
- copies of the parameter values
- used to initialize the object’s attributes
- type of values in arguments needs to be the same as the type of the formal parameter
- Cannot assign an
int
to be aString
value
- Cannot assign an
- The order of arguments needs to be consistent
Summary: 2.2.8
Constructors
initialize attributes in newly created objects- Have the same name as the class
Constructor signature
is the constructor name followed by the parameter listoverloading
is when more than one constructor is present- must differ in number, type, or order of parameters
new
is a keyword to create a new object of a classnew ClassName()
no argument constructor
is a constructor which doesn’t take any passed-in valuesparameters
allow values to be passed in to initialize newly created attributesparameter list
is a list of the type and name of variable being passed in the header of a constructor- these are
formal parameters
- these are
actual parameters
are the values being passed to a constructorformal parameters
are being set to the actual parameters
formal parameters
are the specification of parameters in constructor heading- a list of the type and name of each parameter
World(int width, int height)
- a list of the type and name of each parameter
call by value
is the process by which a value passed to a constructor/method is copied to be used in that constructor/method
AP Practice: 2.2.9
No notes here; just complete the practice.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.