240 likes | 430 Views
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.
E N D
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.
The constructor method has the same name of the class: public class Rectangle { private double dblLength; private double dblWidth; public Rectangle(double dblPLength, double dblPWidth) { dblLength = dblPLength; dblWidth = dblPWidth; } }
A constructor’s header does not specify a return type (not even void). • Why? Constructors are a special type of method. • They are only called once for an object’s creation, and cannot return a value.
3 Types of Constructors: • Default Constructors • Defined Constructors • No Argument Constructors
1. Default Constructors • Default Constructors: • When an object is created, its constructor is ALWAYS called. • Even if you don’t write a constructor for an object, Java automatically provides one when the class is compiled. • The default constructor doesn’t accept arguments. • It sets all of the data fields to 0, false, or null.
1. Default Constructors • Example code from a class with a 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. }
1. Default Constructors • The code to create in instance of a class looks like this: ReportCard_DefaultC rcard1 = new ReportCard_DefaultC(); • These are the values that the default constructor places in the variables:
2. Defined Constructors • Defined Constructors: • You define the constructor inside the class file. • This is very similar to writing a code for a method. • The defined constructor is given parameters. • Likewise, the statement that creates the object must pass arguments to the constructor. If it doesn’t, there will be an error!
2. Defined Constructors • Example code from a class with a defined constructor: Notice how the constructor has the same name as the class. 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; } } Parameters
2. Defined Constructors • The code to create in instance of a class looks like this: ReportCard_DefinedC rcard2 = new ReportCard_DefinedC(0.3, 1.2, 0.7, "Lady McShavers"); • These are the values that the default constructor places in the variables: Arguments
3. No Argument Constructors • No Argument Constructors: • This is a constructor that does not accept arguments. • This type of constructor still initializes variables of the instance, but those values come from the constructor itself. • No-Arg constructors are very useful if you want each instance of a class to have a set value. • For example, what if we wanted to set all of GPAs in every Report Card instance to 4.0 at first? (You know, give kids a good start!)
3. No Argument Constructors • Example code from a class with a No-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 class will be “constructed” with these values.
3. No Argument Constructors • The code to create in instance of a class looks like this: ReportCard_NoArgC rcard3 = new ReportCard_NoArgC(); • These are the values that the default constructor places in the variables: Notice! No Arguments
UML Diagrams • Default Constructors are left out:
UML Diagrams • Defined Constructors are shown with their parameters. Constructors do not have a return type – not even “void,” so don’t put that on there.
UML Diagrams • No Argument Constructors are shown, but there are no arguments to display.