1 / 17

Constructors

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.

Download Presentation

Constructors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructors

  2. 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.

  3. 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; } }

  4. 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.

  5. 3 Types of Constructors: • Default Constructors • Defined Constructors • No Argument Constructors

  6. 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.

  7. 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. }

  8. 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:

  9. 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!

  10. 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

  11. 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

  12. 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!)

  13. 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.

  14. 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

  15. UML Diagrams • Default Constructors are left out:

  16. 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.

  17. UML Diagrams • No Argument Constructors are shown, but there are no arguments to display.

More Related