constructors

34
Constructors and this

Upload: shravani2191

Post on 20-Aug-2015

153 views

Category:

Education


3 download

TRANSCRIPT

Constructors and this

Constructors and this 2

Types in Java

before discussing constructors, we need to look briefly at data types in Java

in Java, every variable has a type the assignment operator (=) is

used to assign values to variables

Using Objects 3

Types in Java

the following statement declares examGrade to be an int variable and assigns examGrade a value of 93

int examGrade = 93;

Using Objects 4

Declaring and Initializing

the statement int examGrade = 93;

could also be written asint examGrade;examGrade = 93;

declaration

assign value

Using Objects 5

Primitive Types

numbers in Java are examples of primitive types

the variable examGrade does not represent an object int is not a class int is a primitive type

Using Objects 6

Object Variables

object variables can be declared in the same way as variables for primitive types Dog myDog; String myString;

object variables differ from primitive values in that they hold a reference to an object, rather than the actual value of the object

Using Objects 7

Reference Variables

examGrade is a variable that stores a primitive type

myDog and myString are reference variables

93

examGrade myDog

A Dog Object

myString

"Hello World!"

Constructors and this 8

The new Operator

use the new operator to create a new object

Rectangle myBox = new Rectangle(); the number and type of

parameters determines the constructor to be called

note: the above statement would be placed in a driver program; it would invoke a constructor from the Rectangle class

Constructors and this 9

The new Operator

when the new operator is used memory is allocated for the object attributes are assigned default values

false is assigned to boolean fields 0 is assigned to numeric fields null is assigned to object references

the constructor is then called to do any user-specified initialization

Constructors and this 10

Constructors

constructing a specific object is called instantiation

the constructor normally initializes the instance variables of the object

a class may have more than one constructor

Constructors and this 11

Rectangle Constructors

let’s take another look at the Rectangle class

if you look at the Java API for the Rectangle class, you will find several constructors, a couple of which are shown on the next slide

Constructors and this 12

Rectangle Constructors

Rectangle(int x, int y, int width, int height) constructs a Rectangle with upper-

left corner at (x, y), with width and height as specified by the last two parameters

Rectangle() constructs a Rectangle with upper-

left corner at (0,0), and with width and height of 0

Constructors and this 13

No-arg Constructors

the second constructor on the previous slide has no parameters

such a constructor is called a no-arg constructor

no-arg constructors are frequently used to assign default values to the instance variables

Constructors and this 14

No-arg Constructors

although not required, it is generally advisable to supply a no-arg constructor for any class you define

Constructors and this 15

The Default Constructor

it is permissible to define a class without any constructors

for classes with no constructors Java provides a no-arg constructor, called the default constructor

the default constructor does nothing

Constructors and this 16

The Default Constructor

if you provide some constructors with arguments, and you also want a no-arg constructor, you must provide it yourself

the default constructor provided by Java is only available if no constructors are provided in the class

Constructors and this 17

The new Operator Revisited if no constructor is provided,

except for the default constructor, the new operator creates a new object with the default values assigned to the attributes; then the default constructor is called the default constructor does nothing,

but new must invoke a constructor, so the default constructor will be used if there is no user-supplied constructor

Using Objects 18

Constructing Strings

there are some shortcuts for String objects that enable us to create strings very easily String myString = "Hello World!" String myString2 = myString;

the previous statements result in two String objects named myString and myString2, both referencing the same String object "Hello World!"

Using Objects 19

myString and myString2

myString myString2

"Hello World!"

Constructors and this 20

Invoking Methods

objects are normally created and used in a driver program

when invoking a method from a driver program you must supply an object reference

otherwise the compiler will not be able to determine which object is the receiver object

Constructors and this 21

Example

public class Driver… Dog zelda = new Dog( "Zelda", "Standard Poodle", 7 ); Dog midge = new Dog( “Midge", "Standard Poodle", 5 );

Constructors and this 22

Example

setAge( 9 ); System.out.println(getAge());

zelda.setAge( 9 );System.out.println( zelda.getAge());

WRONG!these method calls have no object reference; are we trying to set and get the age for zelda or for midge?

Do this instead -- use an object reference

Constructors and this 23

Implicit and Explicit Parameters

in the example given above, zelda is an explicit object reference to the object whose setAge and getAge methods are being invoked

inside a class definition, implicit parameters may be used to refer to instance variables and to invoke methods

Constructors and this 24

Implicit Parameters

consider the following implementation of the setAge method

public void setAge( int ageIn ){ age = ageIn;}The private instance variable age must be associated

with an object, but there is no object reference given.What object does age belong to?

Constructors and this 25

Implicit Parameters

let’s return to the call to setAge from the driver programzelda.setAge( 9 );

zelda is an explicit object reference

the argument 9 is an explicit parameter

Constructors and this 26

Implicit Parameters

this call invokes zelda’s setAge method in the Dog class

inside the setAge method the assignment statement is executed age = ageIn;

zelda is the implicit parameter used

the age field belonging to the object zelda is updated

Constructors and this 27

Implicit Parameters

when is it legal to use implicit parameters?

when a main method creates an object and then calls one of that object’s methods, it must supply an object reference, as in

zelda.setAge( 9 );

Constructors and this 28

Implicit Parameters

inside a class definition, if one method invokes another method, an implicit reference can be used

in this case, the implicit reference used by the first method will also be used for any method call made by the first method

Constructors and this 29

The this Keyword

the implicit parameter used with a method or private instance variable can be accessed with the keyword this

for example, the body of the setAge method could be written as

this.age = ageIn;

Constructors and this 30

this And Constructors

it is permissible to have one constructor call another constructor

suppose we have a Dog class with the two constructors shown on the next slide

Constructors and this 31

this and Constructors

public Dog(String nameIn, String breedIn, int ageIn) { name = nameIn; breed = breedIn; age = ageIn; } public Dog() { name = "unknown"; breed = "unknown"; age = 0; }

Constructors and this 32

this And Constructors

we can use the this keyword to rewrite the no-arg constructor for the Dog class

// no-arg constructorpublic Dog(){ this(“unknown“, “unknown“, 0);}

Constructors and this 33

this And Constructors

the command this(“unknown“, “unknown“, 0); tells the compiler to call a

constructor with the appropriate signature and supply the values of the arguments for the parameters in the called constructor

Constructors and this

The End