what are the types of constructer explain with example

Answer Posted / srinu

There are Two types of constructors
1)Parameterized constructor in which give the parameter
EX:-
class A1
{

A1(int a)
{
System.out.println("HAI HOW ARE U");
}
}
public class A
{
public static void main(String k[])
{
A1 a1=new A1(5);
}
}
2)Default Constructor :- There are no argument in the
constructor is called Default Constructor
class A1
{

A1()
{
System.out.println("HAI ");
}
}
public class A
{
public static void main(String k[])
{
A1 a1=new A1();
}
}

Note: In our java class their is constructor JVM will create
one default constructor.If our class contain default
constructor or parameter constructor or both then that
time JVM will not create Default constructor

Is This Answer Correct ?    5 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is cdi in java?

480


I want to control database connections in my program and want that only one thread should be able to make database connection at a time. Define how can I implement this logic?

579


What is type inference? Is type inference available in older versions like java 7 and before 7 or it is available only in java se 8?

504


Write a program using call by refernce for two different classes to explain to print whether a given number is automorphic or not.

1365


What is http session in java?

486






What is meant by annotations in java?

480


How do I enable jvm?

468


What are jdk tools?

468


How to reverse the singly linked list(In Node data members are(int data,int pointerTONext))

1351


What are jpa annotations?

477


What is cache in java?

502


Where is java installed?

470


What are the new features about Java se 8 ?

518


What is cookie in java?

477


What are microservices in java?

455