0% found this document useful (0 votes)
6 views6 pages

PJ 4

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)
6 views6 pages

PJ 4

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/ 6

Dt : 1/8/2023

*imp

Constructors in Java:

=>Constructor is a method having same name of class and executed while object

creation process,because constructor call is available in Object creation syntax

i
attached with "new" keyword.

thi
=>while declaring constructors we must not use return_type,because the
constructors

ipa
will have Class_return_type.

structure of Constructor:
Ma
sh
Class_name(para_list)

{
ate

//method_body

}
nk
Ve

=>Based on parameters the constructors are categorized into two types:

1.Constructors without parameters

2.Constructors with parameters

1.Constructors without parameters:


=>The constructors which are declared without parameters are known as 0-
parameter

Constructors or Constructors without parameters.

Ex-program : DemoCon1.java

import java.util.Scanner;

i
thi
class Display

ipa
int x,y;

static Display() Ma
{

System.out.println("****Constructor-Display()*****");
sh
System.out.println("The value x:"+x);

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


ate

void dis()
nk

{
Ve

System.out.println("****Method-dis()*****");

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

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

}
class DemoCon1

public static void main(String[] args)

Scanner s = new Scanner(System.in);

i
Display d = new Display();//Con_call

thi
System.out.println("Enter the value of x:");

d.x = s.nextInt();

ipa
System.out.println("Enter the value of y:");

d.y = s.nextInt();

d.dis();//method_call
Ma
d.dis();//method_call
sh
d.dis();//method_call
ate

d.dis();//method_call
nk

}
Ve

o/p:

****Constructor-Display()*****

The value x:0

The value y:0

Enter the value of x:


12

Enter the value of y:

13

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

The value x:12

i
The value y:13

thi
Execution flow of above program:

ipa
ClassFiles:

Display.class

DemoCon1.class(MainClass)
Ma
sh
ate
nk
Ve

===================================================================
=====
faq:

wt is the diff b/w

(i)Constructor

(ii)Instance method

=>Constructor executed while object creation,but Instance method is executed


after

i
thi
Object creation.

=>Constructor executes only once while Object creation process,but Instance

ipa
method

can be executed any number of times after Object creation.


Ma
===================================================================
========

faq:
sh
define static constructor in Java?
ate

=>There is no concept of static constructor in Java,because constructor means

executed while object creation process and cannot be class-level-component.


nk

(Compiletion Error)

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

===========

faq:

define default Constructor?

=>The constructor without parameters which is added by the compiler at


compilation
stage is known as "default constructor".

faq:

In wt situation default constructor is added?

=>when compiler finds any class without any constructors then compiler will add

i
deafault Constructor.

thi
===================================================================
============

ipa
Ma
sh
ate
nk
Ve

You might also like