-3

What is the purpose of the default constructor in java

class Bike1 {
    Bike1() {
        System.out.println("Bike is created");
    }
    public static void main(String args[]){
        Bike1 b=new Bike1();
    }
}
Ctx
  • 18,090
  • 24
  • 36
  • 51
  • 1
    You can use it to assign default values to the class members – Thiyagu Apr 20 '15 at 04:32
  • 3
    possible duplicate of [JAVA-default no argument constructor?](http://stackoverflow.com/questions/3078389/java-default-no-argument-constructor) – AndroidEx Apr 20 '15 at 04:33
  • If you are defining a no-args constructor than it is not a default constructor – Abhishek Apr 20 '15 at 04:36
  • possible duplicate of [Java default constructor](http://stackoverflow.com/questions/4488716/java-default-constructor) – Vogel612 Sep 21 '15 at 07:55

7 Answers7

1

The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
1

Your example provides a constructor,

Bike1(){System.out.println("Bike is created");}

which means you do not get a default constructor. A default constructor is inserted if you do not provide any constructor. Finally, Bike1 is a no-args constructor with package level (or default) access permission and appears to display a message when an instance of Bike1 is created.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Default constructor means that when you don't create any constructor for your class, the compiler automatically creates a default constructor (with no parameters) to your class at the time of compilation.

In your example you created a constructor. The constructor does not create any objects, it initialize the object.

csalmhof
  • 1,820
  • 2
  • 15
  • 24
Rasathurai Karan
  • 673
  • 5
  • 16
0

Default constructors allow you to create objects with known, default settings and behavior. If you call a constructor with arguments, you are creating a custom object. But calling the default constructor will create objects with identical properties every time it is used.

Typically, a default constructor with "no code" doesn't need any code; it already has all the information it needs to create the object.

Remember that default constructor and a constructor with no-args are different. Since you are defining a constructor Bike1(){} here, the default constructor will loose it's scope and will not be generated automatically.

Abhishek
  • 878
  • 12
  • 28
0

The default constructor is the no-argument constructor automatically generated unless you define another constructor. It initialises any uninitialised fields to their default values... follow this link.. Java default constructor

Community
  • 1
  • 1
Zealous System
  • 2,080
  • 11
  • 22
0

Default constructor have no arguments(parameters) and constructor name is same as class name.It will invoke at the time of object creation.

Example:

class Display{

Display(){

System.out.println("Default Constructor");

}

}

class constructor{

public static void main(String args[]){

Display dis=new Display();

}

}

Output:

Default Constructor

Because when the time of object creation default constructor will invoke automatically.

Indhu
  • 109
  • 1
  • 2
  • Hello and Welcome to Stack Overflow. This answer adds nothing new over the already existing answers and accordingly isn't really of much help. You may want to revisit the [tour] and [answering help](https://stackoverflow.com/help/answering) – Vogel612 Sep 21 '15 at 07:58
0

First thing we need to know that default constructor and no argument constructor are both different things. The no argument constructor is one which we declare inside the class where we actually may or may not write the functionality. The default constructor is one which will be called after object creation. The main purpose of this is to initialize the attributes of the object with their default values.

Java compiler provides a default constructor by default if there is no constructor available in the class