Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Class Constructors

1
Initializing objects
 Currently it takes 3 lines to create a
Point and initialize it:
Point p = new Point();
p.x = 3;
p.y = 8; // tedious

 We'd rather pass the fields' initial


values as parameters:
Point p = new Point(3, 8); // better!
 We are able to this with most types of
objects in Java.
2
Constructors
 constructor: Initializes the state of new
objects.
public type(parameters) {
statements;
}
 runs when the client uses the new keyword
 does not specify a return type;
it implicitly returns the new object being created

 If a class has no constructor, Java gives it a default


constructor with no parameters that sets all fields to
0.
3
Constructor example

class Circle{
double r;
}

Circle c2 = new Circle(); // OK, default


constructor

4
Constructor example
class Circle{
double r;
public Circle (double rd) {
r = rd;
}
}
Class CircleTest
{
public static void main(String[] args){
Circle c = new Circle(2.0); //OK
}
}

5
Constructor example
class Circle{
double r;
public Circle()
{ r =10;}
public Circle (double rd) {
r = rd; //same name!
}
}
Class CircleTest{
public static void main(String[] args){
Circle c1 = new Circle(2.0); //OK
Circle c2 = new Circle(); //OK

}
Multiple
} constructor 6

now!!
Overloading Methods and
Constructors

 Two or more methods in a class may


have the same name as long as their
parameter lists are different.

 When this occurs, it is called method


overloading. This also applies to
constructors.

7
Constructor Overloading

 Method overloading is important because


sometimes you need several different ways to
perform the same operation.

 In most situations, you will want to generate


objects of a class from different sets of initial
defining data.

8
Constructor Overloading

package TimoSoft;

public class MyClass


{
int x;
MyClass()
{

x=0;
}
MyClass(int i)
{
x=i;
}

9
Constructor Overloading

MyClass(double d)
{

x=(int)d;
}
void getXvalue()
{
System.out.println("The value of the instance variable is"
+ x + ".");
}
}

10
Constructor Overloading

package TimoSoft;

public class MyClassTest


{

public static void main(String[] args)


{
MyClass first=new MyClass();
MyClass second=new MyClass(52);
MyClass third=new MyClass(13.6);
MyClass fourth = new MyClass(15.5);
first.getXvalue();
second.getXvalue();
third.getXvalue();
}
}
11
Note: Default and No-Argument
Constructors
 Every class must have at least one
constructor
 If no constructors are declared, the
compiler will create a default constructor
 Takes no arguments and initializes instance
variables to their initial values specified in
their declaration or to their default values
 Default values are zero for primitive numeric
types, false for boolean values and null for
references

12
Common constructor bugs
1. Re-declaring fields as local variables ("shadowing"):
public Point(int initialX, int initialY) {
int x = initialX;
int y = initialY;
}
 This declares local variables with the same name as the
fields, rather than storing values into the fields. The fields
remain 0.

13
Common constructor bugs
.2. Accidentally giving the constructor a return type:
public void Point(int initialX, int initialY) {
x = initialX;
y = initialY;
}
 This is actually not a constructor, but a method named
Point

14
Common constructor bugs
3. Missing Assignment to data:
public Point(int initialX, int initialY) {
initialX = x;
initialY = y;
}
 This declares local variables with the same name as the
fields, rather than storing values into the fields. The fields
remain 0.

15
END
Create Time class with four overloaded
constructors.

Create three objects in the runner using


any three of the constrcutors

16

You might also like