(Lec-15) Java SE (Explicit, Initializers, Constructors)
(Lec-15) Java SE (Explicit, Initializers, Constructors)
(CORE JAVA)
LECTURE-15
Today’s Agenda
• Explicit initialization.
• Using constructors
• Parameterized constructors
• In Java to initialize an object we have 3 options, Using
methods is also one of them, which have studied in the
previous lecture.
1. Explicit initialization.
2. Using constructors
3. They are automatically called as soon as an object is created i.e. via new
keyword.
4. If the programmer does not provide any constructor then Java has its
own default constructor in every class with an empty body.
class Account
{
private int accid;
private String name;
private double balance;
public Account()
{
accid=101;
name=“AMIT”
balance=50000.0;
}
public void show()
{
System.out.println(accid+”\n”+name+”\n”+balance);
}
}
Constructors
class CreateAccount
{
public static void main(String [] args)
{
Account A;
A=new Account(); Constructor gets called
A.show();
}
}
* Internally constructors return address of the
object.
Creating Parameterized
Constructors