Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an object. The constructor name always matches the class name -- exactly!
Default Constructor Java always provides a default constructor in every class. You do not have to declare one. It provides parameter-less default constructor that will initialize each instance variable to a default value. double – 0.0 int 0 boolean – false String – null A default constructor would look like below: public Dog() { } public Turtle() {} public SuperHero() {}
Creating your own Constructors Visibility is always public so you can create an object. Example below public class Dog public Dog() { } public class Turtle public Turtle() { } Format for default constructor: visibility ClassName () { }
Initializing Data in the Constructor Default constructors could also be used to initialize a value to instance variables Public class Name { private String name; public Name() { name = “My name”; }
When you create a constructor – you lose the default Remember Java does provide a default constructor if you do not put a constructor in your program. If you type a constructor in your program Java will no longer provide the default. Therefore when you create overloaded constructors for your program, you will need to also create the default if you need that format.
Overloaded Constructor Format: visibility ClassName(dataType variableName) When you create an overloaded constructor the data being passed through the constructor will be used to initialize the instance variables. You will create variables that will be passed through the parameter that will then initialize the instance variables. Example on next slide.
public class SuperHero { private String name; private String power; private int speed; private String weapon; public SuperHero(String n, String p, int s, String w) { name = n; power = p; seed = s ; weapon = w; } Let’s create a SuperHero class. SuperHeros usually all have a name, power, speed, weapon, however this information is different for each SuperHero object created. The information passed through the parameter will initialize the instance variables.
Creating an object from a default constructor When the object is created you always use the keyword new. Creating an object from a default constructor: ClassName object = new ClassName(); Dog d = new Dog(); Turtle t = new Turtle(); Bug b = new Bug();
Creating object from an overloaded constructor The overloaded constructor requires that data be passed when the object is created. It must be passed in the exact order listed in the parameter. public SuperHero(String n, String p, int s, String w) { name = n; power = p; seed = s ; weapon = w; } SuperHero s = new SuperHero(“Iron Man”, “Suit”, 400, “Repulsor rays”
Practice Constructor Object ____________________ public Dog() Dog d = new Dog(); public Dog(String name) Dog d = new Dog(“Lassie”); public Rectangle(double width, double height) Rectangle r = new Rectangle(15, 20);
Summary Java provides a default constructor for every class. If you create a constructor you will lose the default Constructors are always public Constructors do no return anything Constructors always start with a curly brace and end with a curly brace Default constructors have an empty parameter Overloaded constructors pass information through the parameter You must declare the datatype and the variable name in the parameter for an overloaded constructor The keyword for creating an object is new When you create an object from an overloaded constructor you pass information through the parameter in the order specified by the constructor.