-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
  • 17,568
  • 24
  • 34
  • 49
  • 1
    You can use it to assign default values to the class members – user7 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

6 Answers6

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,776
  • 5
  • 26
  • 37
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
  • 191,680
  • 20
  • 149
  • 239
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,679
  • 2
  • 15
  • 23
Rasathurai Karan
  • 322
  • 3
  • 11
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
  • 868
  • 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,059
  • 9
  • 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