Docsity

Constructor - Java - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Constructor - Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Programming Language โ€“ Java

Road map today Brief review Details of class^ 

Constructor  this^

reference Inheritance Overloading Dynamic binding Interface Exceptions

Object and class A class is a blueprint An object is an instance created from thatblueprint All objects of the same class have thesame set of attributes^ 

Every Person object have name, weight, height But different value for those attributes^ 

ke.name=Ke Wang, sal.name=Sal Stolfo

Class Person: illustration

Name: Ke Wangheight: 0weight: 0 Name: Salvatore J. Stolfoheight: 0weight: 0

ke sal

Class Person: variables

Name: Ke Wangheight: 0weight: 0 Name: Salvatore J. Stolfoheight: 0weight: 0

ke x sal references

objects

Visibility of fields and methods Generally make fields

private

and provide

public

getField() and setField() accessor functions O-O term: encapsulation Private fields and methods cannot beaccessed from outside of the class.

Static vs. non-static (cont.) Instance fields define an object; the valuesof those fields make one object distinctfrom another Instance method operates on an instanceof a class (object) instead of operating onthe class itself. Class methods can only use class fields;while instance methods can use bothinstance fields and class fields

How instance method works? How can the method know whether itโ€™sbeen called for object a or b?^ 

Internal: Person.setWeight(a, 100); Invisible additional parameter to allinstance methods:

this

It holds a reference to the object throughwhich the method is invoked^ 

a.setWeight(100)

^ this=a

Person a=new Person(), b=new Persion();a.setWeight(100);

b.setWeight(120);

Keyword

this

example I

class A{

int^ w ; public void setValue (int

w ) { this. w

=^ w ; //same name! } } When a method parameter or local variable in amethod has the same name as one of the fieldsof the class, you must use this to refer to the field.

Keyword

this

example II

class Exp{

public int i=0;public Exp increment () {

i++;return

this ; // return current object } public static void main (String[] args){

Exp e = new Exp();int v = e.increment().increment().i; // v=2!! } }

Constructor A special method automatically calledwhen an object is created by new() Java provide a default one that takes noarguments and perform no specialinitialization^ 

Initialization is guaranteed All fields set to default values: primitive types to0 and false, reference to null

Constructor (cont.) Must have the same name as the classname^ 

So the compiler know which method to call ^ Perform any necessary initialization ^ Format:

public ClassName(para){โ€ฆ} ^ No return type, even no void!^ 

It actually return current object ^ Notice: if you define any constructor, withparameters or not, Java will not create thedefault one for you.

Constructor exampleclass Circle{

double r;public Circle (double r) {

this.r = r; //same name! } public static void main(String[] args){

Circle c = new Circle(2.0); //OKCircle c2 = new Circle(); //error!!, no more default } } Circle.java:8: cannot resolve symbolsymbol : constructor Circle ()location: class Circle

Circle c2 = new Circle(); //error!!

^

1 error

Constructor exampleclass Circle{

double r;public Circle(){

r = 1.0; //default radius value; } public Circle (double r) {

this.r = r; //same name! } public static void main(String[] args){

Circle c = new Circle(2.0); //OKCircle c2 = new Circle();

// OK now!

} }

Multiple constructor now!!