Chapter 4 Constructors Section 4.4

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 4 Defining Classes I Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
SWE 510: Object Oriented Programming in Java1 Defining Classes 1 This slide set was compiled from the Absolute Java textbook and the instructor’s own class.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
ECE122 Feb. 22, Any question on Vehicle sample code?
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Slides prepared by Rose Williams, Binghamton University Chapter 4 Defining Classes I.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10 Introduction to File I/O Section 10.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Polymorphism and Abstract Classes
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Road Map Introduction to object oriented programming. Classes
Chapter 2 Screen Output Section 2.1
Chapter 19 Java Never Ends
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 7 Functions of Random Variables (Optional)
Chapter 14 Graphs and Paths.
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Chapter 10 Datapath Subsystems.
Interfaces and Inner Classes
Chapter 20 Hash Tables.
Inheritance 2nd Lecture
CS139 October 11, 2004.
Chapter 5 Algorithm Analysis.
Chapter 4 Defining Classes I
Classes and Objects Static Methods
The Facts to Be Explained
Classes, Objects and Methods
Introduction: Some Representative Problems
Chapter 3 Debugging Section 3.4
Information Hiding and Encapsulation Section 4.2
Section 2.3 Introduction to File Input
Multidimensional Arrays Section 6.4
Circuit Characterization and Performance Estimation
CMSC 202 Inheritance II.
Chapter 6 Dynamic Programming.
Classes and Objects Object Creation
Chapter 2 Reference Types.
Chapter 8 Abstract Classes.
Chapter 4 Greedy Algorithms.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Chapter 4 Constructors Section 4.4 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Constructors A constructor is a special kind of method that is designed to initialize the instance variables for an object: public ClassName(anyParameters){ code } A constructor must have the same name as the class A constructor has no type returned, not even void Constructors are typically overloaded Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Constructors A constructor is called when an object of the class is created using new ClassName objectName = new ClassName(anyArgs); The name of the constructor and its parenthesized list of arguments (if any) must follow the new operator This is the only valid way to invoke a constructor: a constructor cannot be invoked like an ordinary method If a constructor is invoked again (using new), the first object is discarded and an entirely new object is created If you need to change the values of instance variables of the object, use mutator methods instead Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

You Can Invoke Another Method in a Constructor The first action taken by a constructor is to create an object with instance variables Therefore, it is legal to invoke another method within the definition of a constructor, since it has the newly created object as its calling object For example, mutator methods can be used to set the values of the instance variables It is even possible for one constructor to invoke another Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

A Constructor Has a this Parameter Like any ordinary method, every constructor has a this parameter The this parameter can be used explicitly, but is more often understood to be there than written down The first action taken by a constructor is to automatically create an object with instance variables Then within the definition of a constructor, the this parameter refers to the object created by the constructor Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Include a No-Argument Constructor If you do not include any constructors in your class, Java will automatically create a default or no-argument constructor that takes no arguments, performs no initializations, but allows the object to be created If you include even one constructor in your class, Java will not provide this default constructor If you include any constructors in your class, be sure to provide your own no-argument constructor as well Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Default Variable Initializations Instance variables are automatically initialized in Java boolean types are initialized to false Other primitives are initialized to the zero of their type Class types are initialized to null However, it is a better practice to explicitly initialize instance variables in a constructor Note: Local variables are not automatically initialized Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Copyright © 2012 Pearson Addison-Wesley. All rights reserved.