Constructors - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Constructors

Description:

Never marked with a return type. Written as public methods ... Dog milo = new Dog(3,62); Arguments 3,62 are the age and weight. Notice order is important ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 8
Provided by: swea9
Category:

less

Transcript and Presenter's Notes

Title: Constructors


1
Constructors
  • Java Programming Chapter 3

2
Constructors
  • Constructors are a special kind of Method that
    create an instance of your class
  • Responsible for initializing your class

3
Rules for Creating Constructors
  • Always shares the same name as the class
  • May take parameters
  • Never marked with a return type
  • Written as public methods
  • Use a set of initial values for the class member
    fields

4
Class Dog private int age private int
weight private Dog(int howOld, int
howBig) agehowOld weight howBig
Constructor
Set values
5
Dog
  • Notice
  • Dog class and constructor class have the same
    name
  • Constructor has two parameters
  • int howOld
  • int howBig

6
Assignment
  • howOld is assigned to age
  • howBig is assigned to weight

7
New Dog Object
  • To create an instance of a Dog object
  • Combine the constructor with new operator
  • Keyword new used to create a new instance of the
    class
  • Dog milo new Dog(3,62)
  • Arguments 3,62 are the age and weight
  • Notice order is important
Write a Comment
User Comments (0)
About PowerShow.com