0% found this document useful (0 votes)
21 views

(Lec-15) Java SE (Explicit, Initializers, Constructors)

The document discusses different ways to initialize objects and data members in Java classes, including explicit initialization where members are initialized at declaration, using constructors which are automatically called when an object is created, and parameterized constructors that allow passing parameters to initialize members. It provides examples of each method using a sample Account class and explains how to create a Circle class that initializes the radius member through a parameterized constructor and includes methods to calculate area and circumference.

Uploaded by

malise2997
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

(Lec-15) Java SE (Explicit, Initializers, Constructors)

The document discusses different ways to initialize objects and data members in Java classes, including explicit initialization where members are initialized at declaration, using constructors which are automatically called when an object is created, and parameterized constructors that allow passing parameters to initialize members. It provides examples of each method using a sample Account class and explains how to create a Circle class that initializes the radius member through a parameterized constructor and includes methods to calculate area and circumference.

Uploaded by

malise2997
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA SE

(CORE JAVA)
LECTURE-15
Today’s Agenda

• Initializing Objects or Data member

• 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. Using initializer blocks.


Explicit Initialization

• Java permits us to initialize members of the class at the


point of their declaration and such way of initialization
is known as Explicit initialization.

• C++ considers this way as error, but in Java this is


considered the fastest way of initialization.

• This is preferred when all objects should have the same


value at initialization.

• Let us take an example of a class named Account.


Explicit Initialization
class Account
{
private int id=101;
private String name=“AMIT”;
private double balance=50000.0;
public void show( )
{ 1000
S.O.P(id+“\n”+name+“\n”+balance); 101
0
}
} 1000 2000
0 A M I T
class UseAccount A 2000
{ 50000
0
public static void main(String [ ] args)
{
Account A;
A=new Account( );
A.show( );
}
Constructors

• Constructors are special methods of a class with following


characteristics

1. They have the same name as that of the class.

2. They don’t have any return type.

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.

5. There are no copy constructors provided by Java. They can be made by


programmers.
Constructors

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

• It is same as creating a parameterized method, but while


creating the object using new, programmer has to make sure
that he passes those same parameters.
class Account
{
private int accid;
private String name;
private double balance;
public Account(int id, String s, double b)
{
accid=id;
name=n;
balance=b;
}
Creating Parameterized
Constructors
public void show()
{
System.out.println(accid+”\n”+name+”\n”+balance);
}
}
class CreateAccount
{
public static void main(String [] args)
{
Scanner kb=new Scanner(System.in);
S.O.P(“Enter Account id, name and balance”);
int id=kb.nextInt();
String name=kb.nextLine();
double balance=kb.nextDouble();
Account A;
A=new Account(id,name,balance);
A.show();
}
}
Exercise

• WAP which consists of a class named Circle, with a


data member radius. Accept radius as an input from
the user and initialize the data member through
parameterized constructor. Include two more
methods area() and circumference() which
calculates area and circumference and prints them.
End Of Lecture 15

For any queries mail us @: scalive4u@gmail.com


Call us @ : 0755-4271659, 7879165533

Agenda for Next Lecture:


1. Creating array of references
2. Initializer blocks.
3. Method Overloading

You might also like