17
Constructors Constructors

Constructors

Embed Size (px)

DESCRIPTION

Constructors. Constructor: A method that is automatically called when an instance of a class is created. Usually performs initialization operations, such as storing defined values in instance fields. Called a “constructor” because it helps CONSTRUCT an object. - PowerPoint PPT Presentation

Citation preview

Page 1: Constructors

ConstructorsConstructors

Page 2: Constructors

Constructor:Constructor:

• A method that is automatically called when an A method that is automatically called when an instance of a class is created.instance of a class is created.

• Usually performs initialization operations, such Usually performs initialization operations, such as storing defined values in instance fields.as storing defined values in instance fields.

• Called a “constructor” because it helps Called a “constructor” because it helps CONSTRUCT an object.CONSTRUCT an object.

Page 3: Constructors

The constructor method has the same name of the The constructor method has the same name of the class:class:

public class Rectanglepublic class Rectangle{{

private double dblLength;private double dblLength;private double dblWidth;private double dblWidth;

public Rectangle(double dblPLength, double public Rectangle(double dblPLength, double dblPWidth)dblPWidth)

{{dblLength = dblPLength;dblLength = dblPLength;dblWidth = dblPWidth;dblWidth = dblPWidth;

}}}}

Page 4: Constructors

A constructor’s header does not A constructor’s header does not specify a return type (not even void).specify a return type (not even void).

Why? Constructors are a special type Why? Constructors are a special type of method. of method.

They are only called once for an They are only called once for an object’s creation, and cannot return object’s creation, and cannot return a value. a value.

Page 5: Constructors

3 Types of Constructors:3 Types of Constructors:

1.1. Default ConstructorsDefault Constructors

2.2. Defined ConstructorsDefined Constructors

3.3. No Argument ConstructorsNo Argument Constructors

Page 6: Constructors

1. Default Constructors1. Default Constructors Default Constructors:Default Constructors:

• When an object is created, its constructor is When an object is created, its constructor is ALWAYS called. ALWAYS called.

• Even if you don’t write a constructor for an Even if you don’t write a constructor for an object, Java automatically provides one when the object, Java automatically provides one when the class is compiled.class is compiled.

• The default constructor doesn’t accept The default constructor doesn’t accept arguments.arguments.

• It sets all of the data fields to 0, false, or null.It sets all of the data fields to 0, false, or null.

Page 7: Constructors

1. Default Constructors1. Default Constructors

Example code from a class with a Example code from a class with a default constructor:default constructor:

public class ReportCard_DefaultC{

//Declaring Private Variables private double dblJava; private double dblHistory; private double dblGPA; private String strStudentName;

//There is no constructor defined here! That is because Java will //automatically create one if one is not defined.}

Page 8: Constructors

1. Default Constructors1. Default Constructors The code to create in instance of a class looks like The code to create in instance of a class looks like

this: this:

ReportCard_DefaultC rcard1 = new ReportCard_DefaultC rcard1 = new ReportCard_DefaultC();ReportCard_DefaultC();

These are the values that the default constructor These are the values that the default constructor places in the variables:places in the variables:

Page 9: Constructors

2. Defined Constructors2. Defined Constructors Defined Constructors:Defined Constructors:

• You define the constructor inside the class file. You define the constructor inside the class file.

• This is very similar to writing a code for a This is very similar to writing a code for a method.method.

• The defined constructor is given parameters. The defined constructor is given parameters.

• Likewise, the statement that creates the object Likewise, the statement that creates the object must pass arguments to the constructor. If it must pass arguments to the constructor. If it doesn’t, there will be an error!doesn’t, there will be an error!

Page 10: Constructors

2. Defined Constructors2. Defined Constructors

Example code from a class with a defined Example code from a class with a defined constructor:constructor:

public class ReportCard_DefinedC{

//Declaring Private Variables private double dblJava; private double dblHistory; private double dblGPA; private String strStudentName;

public ReportCard_DefinedC(double dblPJava, double dblPHistory, double dblPGPA, String strPStudentName) { dblJava = dblPJava; dblHistory = dblPHistory; dblGPA = dblPGPA; strStudentName = strPStudentName;

}}

Notice how the constructorhas the same name as the

class.

Parameters

Page 11: Constructors

2. Defined Constructors2. Defined Constructors The code to create in instance of a class looks like The code to create in instance of a class looks like

this: this:

ReportCard_DefinedC rcard2 = new ReportCard_DefinedC(0.3, ReportCard_DefinedC rcard2 = new ReportCard_DefinedC(0.3, 1.2, 0.7, "Lady McShavers");1.2, 0.7, "Lady McShavers");

These are the values that the default constructor These are the values that the default constructor places in the variables:places in the variables:

Arguments

Page 12: Constructors

3. No Argument Constructors3. No Argument Constructors No Argument Constructors:No Argument Constructors:

• This is a constructor that does not accept arguments. This is a constructor that does not accept arguments.

• This type of constructor still initializes variables of the This type of constructor still initializes variables of the instance, but those values come from the constructor instance, but those values come from the constructor itself.itself.

• No-Arg constructors are very useful if you want each No-Arg constructors are very useful if you want each instance of a class to have a set value.instance of a class to have a set value.

• For example, what if we wanted to set all of GPAs in For example, what if we wanted to set all of GPAs in every Report Card instance to 4.0 at first? (You know, every Report Card instance to 4.0 at first? (You know, give kids a good start!) give kids a good start!)

Page 13: Constructors

3. No Argument Constructors3. No Argument Constructors

Example code from a class with a No-Example code from a class with a No-Arg constructor:Arg constructor:

public class ReportCard_NoArgC{

//Declaring Private Variables private double dblJava; private double dblHistory; private double dblGPA; private String strStudentName;

public ReportCard_NoArgC() { dblJava = 4.0; dblHistory = 4.0; dblGPA = 4.0; strStudentName = "Mister Crow"; }}

Each instance of the classwill be “constructed” with these

values.

Page 14: Constructors

3. No Argument Constructors3. No Argument Constructors The code to create in instance of a class looks like The code to create in instance of a class looks like

this: this:

ReportCard_NoArgC rcard3 = new ReportCard_NoArgC();ReportCard_NoArgC rcard3 = new ReportCard_NoArgC();

These are the values that the default constructor These are the values that the default constructor places in the variables:places in the variables:

Notice! No Arguments

Page 15: Constructors

UML DiagramsUML Diagrams

Default Constructors are left out:Default Constructors are left out:

Page 16: Constructors

UML DiagramsUML Diagrams Defined Constructors are shown with their Defined Constructors are shown with their

parameters. Constructors do not have a return parameters. Constructors do not have a return type – not even “void,” so don’t put that on there.type – not even “void,” so don’t put that on there.

Page 17: Constructors

UML DiagramsUML Diagrams No Argument Constructors are shown, but there are no No Argument Constructors are shown, but there are no

arguments to display.arguments to display.