We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20
Chapter 4-2:
More Object Concepts
Overloading Constructors (cont'd.)
Java Programming, Sixth Edition 2
Learning About the this Reference • Instantiate object from class – Memory reserved for each instance field in class – Not necessary to store separate copy of each variable and method for each instantiation of class • In Java – One copy of each method in class stored – All instantiated objects can use one copy
Java Programming, Sixth Edition 3
Learning About the this Reference (cont'd.) • Reference – Object’s memory address – Implicit • Automatically understood without actually being written
Java Programming, Sixth Edition 4
Learning About the this Reference (cont'd.) • this reference – Reference to object – Passed to any object’s nonstatic class method – Reserved word in Java – Don’t need to use this reference in methods you write • In most situations
Java Programming, Sixth Edition 5
Learning About the this Reference (cont'd.)
Java Programming, Sixth Edition 6
Learning About the this Reference (cont'd.) • this reference (cont'd.) – Implicitly received by instance methods – Use to make classes work correctly – When used with field name in class method • Reference to class field • Instead of to local variable declared within method
Java Programming, Sixth Edition 7
Using the this Reference to Make Overloaded Constructors More Efficient • Avoid repetition within constructors • Constructor calls other constructor – this() – More efficient and less error-prone
Java Programming, Sixth Edition 8
Java Programming, Sixth Edition 9 Using static Variables • Class methods – Do not have this reference – Have no object associated with them • Class variables – Shared by every instantiation of class – Only one copy of static class variable per class
Java Programming, Sixth Edition 10
Using Constant Fields • Create named constants using keyword final – Make its value unalterable after construction • Can be set in class constructor – After construction cannot change final field’s value
Java Programming, Sixth Edition 11
Using Constant Fields (cont'd.)
Java Programming, Sixth Edition 12
Using Automatically Imported, Prewritten Constants and Methods • Many classes commonly used by wide variety of programmers • Package or library of classes – Folder provides convenient grouping for classes – Many contain classes available only if explicitly named within program – Some classes available automatically
Java Programming, Sixth Edition 13
Using Automatically Imported, Prewritten Constants and Methods (cont'd.) • Fundamental or basic classes – Implicitly imported into every Java program – java.lang package • Only automatically imported, named package • Optional classes – Must be explicitly named
Java Programming, Sixth Edition 14
Using Automatically Imported, Prewritten Constants and Methods (cont'd.) • java.lang.Math class – Contains constants and methods used to perform common mathematical functions – No need to create instance – Imported automatically – Cannot instantiate objects of type Math • Constructor for Math class private
Java Programming, Sixth Edition 15
Java Programming, Sixth Edition 16 You Do It • Demonstrating scope • Overloading methods • Creating overloaded constructors • Using an explicitly imported prewritten class • Creating an interactive application with a timer
Java Programming, Sixth Edition 17
Don’t Do It • Don’t try to use a variable that is out of scope • Don’t assume that a constant is still a constant when passed to a method’s parameter • Don’t overload methods by giving them different return types • Don’t think that default constructor means only the automatically supplied version • Don’t forget to write a default constructor for a class that has other constructors
Java Programming, Sixth Edition 18
Summary • Variable’s scope – Portion of program in which you can reference variable • Block – Code between a pair of curly braces • Overloading – Writing multiple methods with same name but different argument lists • Store separate copies of data fields for each object – But just one copy of each method Java Programming, Sixth Edition 19 Summary (cont'd.) • static class variables – Shared by every instantiation of a class • Prewritten classes – Stored in packages • import statement – Notifies Java program that class names refer to those within imported class • A class can contain other objects as data members • You can create nested classes that are stored in the same file Java Programming, Sixth Edition 20