You are on page 1of 9

Dt : 21/5/2022

faq:

wt is the advantage of Constructor?

=>Constructors are used to initialize instance variables while

object creation and saves the execution time,and generate High

performance of an application.

===========================================================

Note:

=>we have the following three options to initialize instance

Variables:

(a)Using Object name

(b)Using Setter method

(c)Using Constructor

(a)Using Object name:

=>we can initialize instance variables by accessing using

object name.

Ex:

User.java

package p2;
public class User {
public String fName,lName,addr,mId;
public long phNo;
public void getUser()
{
System.out.println("====UserDetails===");
System.out.println("FirstName:"+fName);
System.out.println("LastName:"+lName);
System.out.println("Address:"+addr);
System.out.println("MailId:"+mId);
System.out.println("PhNo:"+phNo);
}
}

DemoCon4.java

package p1;

import java.util.Scanner;

import p2.User;

public class DemoCon4 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

User ob = new User();//Con_Call

System.out.println("Enter the FirstName:");

ob.fName = s.nextLine();

System.out.println("Enter the LatName:");

ob.lName = s.nextLine();

System.out.println("Enter the Address:");

ob.addr = s.nextLine();

System.out.println("Enter the MailId:");

ob.mId = s.nextLine();

System.out.println("Enter the PhNo:");

ob.phNo = s.nextLong();

ob.getUser();

s.close();
}

Execution Flow:

ClassFiles:

User.class

DemoCon4.class(MainClass)

---------------------------------------------------------

(b)Using Setter method:

=>we declare setter method in class and which is used to set

the data to object or load the data to object.

Ex:

User.java
package p2;
public class User {
public String fName,lName,addr,mId;
public long phNo;
public void setUser(String a,String b,String c,String d,
long e)
{
fName=a;
lName=b;
addr=c;
mId=d;
phNo=e;
}
public void getUser()
{
System.out.println("====UserDetails===");
System.out.println("FirstName:"+fName);
System.out.println("LastName:"+lName);
System.out.println("Address:"+addr);
System.out.println("MailId:"+mId);
System.out.println("PhNo:"+phNo);
}
}

DemoCon5.java(MainClass)

package p1;

import java.util.Scanner;

import p2.User;

public class DemoCon5 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

User ob = new User();//Con_Call

System.out.println("Enter the FirstName:");

String fN = s.nextLine();

System.out.println("Enter the LatName:");


String lN = s.nextLine();

System.out.println("Enter the Address:");

String ad = s.nextLine();

System.out.println("Enter the MailId:");

String md = s.nextLine();

System.out.println("Enter the PhNo:");

long pNo = s.nextLong();

ob.setUser(fN, lN, ad, md, pNo);

ob.getUser();

s.close();

Execution flow:

ClassFiles:

User.class

DemoCon5.class(MainClass)
--------------------------------------------------------

(c)Using Constructor:

=>We use constructor to initialize instance variables while

object creation and which saves the execution time.

Ex:

User.java

package p2;
public class User {
public String fName,lName,addr,mId;
public long phNo;
public User(String a,String b,String c,String d,
long e)//Con_with_parameter
{
fName=a;
lName=b;
addr=c;
mId=d;
phNo=e;
}
public void getUser()
{
System.out.println("====UserDetails===");
System.out.println("FirstName:"+fName);
System.out.println("LastName:"+lName);
System.out.println("Address:"+addr);
System.out.println("MailId:"+mId);
System.out.println("PhNo:"+phNo);
}
}

DemoCon6.java(MainClass)

package p1;

import java.util.Scanner;

import p2.User;

public class DemoCon6 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("Enter the FirstName:");

String fN = s.nextLine();

System.out.println("Enter the LatName:");

String lN = s.nextLine();

System.out.println("Enter the Address:");

String ad = s.nextLine();

System.out.println("Enter the MailId:");

String md = s.nextLine();

System.out.println("Enter the PhNo:");

long pNo = s.nextLong();


User ob = new User(fN,lN,ad,md,pNo);//Con_Call

ob.getUser();

s.close();

Execution Flow:

Class files:

User.class

DemoCon6.class(MainClass)

=======================================================

Assignment-1:

wap to read and display Product details using Constructor?


SubClass : Product

MainClass : ProductMainClass

Assignment-2 :

wap to read and display BookDetails using Constructor?

SubClass : BookDetails

MainClass : BookMainClass

======================================================

You might also like