Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
173 views
in JAVA by (178k points)
edited by
What is a default constructor in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

A default constructor is a constructor in Java that is automatically created by the compiler when a class does not have any constructor defined. This constructor takes no parameters and has an empty body. Its purpose is to initialize the member variables of the class with default values.

For example, consider the following class:

public class MyClass {
   int num;
   String str;
}
 

Since this class does not have any constructor defined, the Java compiler automatically creates a default constructor for it. The default constructor would look like this:

public MyClass() {
}
 

When an object of MyClass is created, the default constructor is called automatically to initialize the member variables num and str to their default values (which are 0 and null, respectively).

It is important to note that if a class has any constructor defined, the compiler does not create a default constructor. In that case, the class only has the constructors that are defined explicitly.

Related questions

0 votes
1 answer
0 votes
2 answers

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...