0% found this document useful (0 votes)
12 views9 pages

PJ 5

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

PJ 5

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dt : 2/8/2023

2.Constructors with parameters:

=>The Constructors which are declared with parameters are known as


parameterized

constructors or Constructors with parameters.

i
thi
Ex-program : DemoCon3.java

import java.util.Scanner;

ipa
class Display3

{ Ma
//x and y are Instance variables memory in Object

int x,y;
sh

Display3(int x,int y)
ate

//x and y are local variables and memory in Constructor

{
nk

this.x=x;
Ve

this.y=y;

void dis()

System.out.println("****Method-dis()*****");
System.out.println("The value x:"+x);

System.out.println("The value y:"+y);

class DemoCon3

i
{

thi
public static void main(String[] args)

ipa
Scanner s = new Scanner(System.in);
Ma
System.out.println("Enter the value-1:");

int v1 = s.nextInt();

System.out.println("Enter the value-2:");


sh
int v2 = s.nextInt();
ate

Display3 d = new Display3(v1,v2);//Con_call

d.dis();//method_call
nk

}
Ve

o/p:

Enter the value-1:

12

Enter the value-2:

13
****Method-dis()*****

The value x:12

The value y:13

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

Execution flow of above program:

i
ClassFiles:

thi
Display3.class

DemoCon3.class(MainClass)

ipa
Ma
sh
ate
nk
Ve

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

Advantage of Constructors:

=>Constructors are used to initialize instance variables while object creation


process,and which saves the execution time and generate HighPerformance of
an

application.

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

faq:

i
thi
define "this" keyword?

=>"this" keyword will hold the reference of object from where constructor or

ipa
method executing.

=>using "this" keyword we can access the members of object.


Ma
=>we use "this" keyword when we are loading data from Local Variables to

Instance variables having the Same name.

===================================================================
sh
=
ate

*imp

Loading data to Objects:


nk

=>we can load the data to Objects in three ways:

1.Using Constructor
Ve

2.Using Object reference variable

3.Using "Setter methods"

1.Using Constructor:

=>We use Constructors to initialize instance variables while object creation


process.

Ex:

DemoCon3.java

2.Using Object reference variable:

i
=>we use Object reference variables(Object names) to load the data to

thi
Objects.

Ex:

ipa
DemoCon1.java

3.Using "Setter methods" :


Ma
=>we can also load the data to objects using "Setter methods"
sh

Ex : DemoCon4.java
ate

import java.util.Scanner;

class Customer
nk

{
Ve

int no;

String name;

void setNo(int no)

this.no=no;
}

void setName(String name)

this.name=name;

i
int getNo()

thi
{

return no;

ipa
}

String getName()

{
Ma
return name;
sh
}
ate

class DemoCon4
nk

public static void main(String[] args)


Ve

Scanner s = new Scanner(System.in);

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

int no = Integer.parseInt(s.nextLine());

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


String name = s.nextLine();

Customer c = new Customer();//Con_call

//calling Setter methods

i
c.setNo(no);

thi
c.setName(name);

ipa
//Calling Geeter methods

int n = c.getNo();
Ma
String nm = c.getName();
sh
System.out.println("CustNo:"+n);
ate

System.out.println("Name:"+nm);

}
nk

o/p:
Ve

Enter the CustNo:

1234

Enter the CustName:

Raj

CustNo:1234
Name:Raj

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

faq:

define Setter methods?

=>The methods which are used to set the data to objects are known as "Setter"

i
thi
methods.

ipa
faq:

define Getter methods? Ma


=>The methods which are used to get the data from the Objects are known as
"Getter"

methods.
sh
ate

Coding rule of writing Setter and Getter methods:

=>Every variable in class must have its own Setter and Getter method.
nk

===================================================================
=========
Ve

Assignment:

wap to read and display UserDetails?

SubClass : UserDetails

Variables : name,mId,phNo
Constructor : UserDetails(name,mId,phNo)

Method : void getUserDetails()

MainClass : DemoUser

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

i
thi
ipa
Ma
sh
ate
nk
Ve

You might also like