answersLogoWhite

0


Best Answer

Correct. If you omit a constructor, Java will assume that an empty one exists.

Given the following code for a class:

class MyClass {

}

You can make a call to the default constructor:

MyClass mc = new MyClass();

Just keep in mind that the default constructor "goes away" if you implement another constructor.

class MyClass {

public MyClass(String str){

}

}

This line will now result in a "cannot find symbol" compiler error:

MyClass mc = new MyClass();

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Determine Whether a Default Constructor Will Be Created

The following example shows a Lamborghini class with two constructors:

class Lamborghini {

Lamborghini() { }

Lamborghini(String name) { }

}

Will the compiler put in a default constructor for the class above? "No!" What about for the following variation of the class?

class Lamborghini {

Lamborghini(String name) { }

}

Now will the compiler insert a default constructor? "No Again!" What about this class?

class Lamborghini { }

Now we're talking. The compiler will generate a default constructor for the preceding class, because the class doesn't have any constructors defined. OK, what about this one below?

class Lamborghini {

void Lamborghini() { }

}

You might be tempted to say that, the compiler won't create one, since there already is a constructor in the Lamborghini class? Take another look at the Lamborghini class.

What's wrong with the Lamborghini() constructor? It isn't a constructor at all! It's simply a method that happens to have the same name as the class. Remember, the return type is a dead straight way to tell us that we're looking at a method, and not a constructor. So, here again the compiler will put the default no-arg constructor in the class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

yes java always provide a default constructor to the class unless you have created a parameterized constructor by yourself for ex if you have created a cons like ABC(int a){ } and after that you try to call a default constructor i.e. ABC() , you may not be allowed to do so as per java rules.

thanks

Mayank chandela

(S.Engg - SDG India Pvt Ltd)

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

You get a default constructor when you do not provide a constructor that takes no arguments. Such a (compiler provided) default constructor will only set up the object (initialize vftables and vbtables) and call constructors for base classes, but it will take no further action, with the result that your object will not be in a properly initialized state.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Any time you don't write any constructor for your class, the compiler supplies the default constructor.

In other words, if you wrote a constructor the compiler won't.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

When the programmer has not coded a constructor for any java class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is Java always provides a default constructor to a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


Why Java always provides a default constructor to class when you use another constructor default constructor remove?

Classes in Java inherit constructors from their parent classes. If you don't explicitly define a parent class, then Object is used, which has only the default empty constructor. That "default" constructor is only there when defined by the parent class, so classes which do not have a no-argument constructor will not allow subclasses to automatically use it. This is implemented this way because of the special nature of constructors. Java could not always provide a default constructor because it could not guarantee that all class members would be properly created or initialized.


What is the difference between default constructor and parameterized constructor?

A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


What is a constructoris it mandatory to use constructor in a class?

You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class. While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior. For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage. For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".

Related questions

Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


Why Java always provides a default constructor to class when you use another constructor default constructor remove?

Classes in Java inherit constructors from their parent classes. If you don't explicitly define a parent class, then Object is used, which has only the default empty constructor. That "default" constructor is only there when defined by the parent class, so classes which do not have a no-argument constructor will not allow subclasses to automatically use it. This is implemented this way because of the special nature of constructors. Java could not always provide a default constructor because it could not guarantee that all class members would be properly created or initialized.


What is the difference between default constructor and parameterized constructor?

A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


What is a constructoris it mandatory to use constructor in a class?

You always should define default constructor for your class. You must also define a copy constructor for your class if there are any pointers in the class. While it is not mandatory, failure to provide a default constructor can result in bad behavior, and failure to provide a copy constructor when you have pointers in the class will result in bad behavior. For example, without a default constructor, the compiler will not fully initialize the attributes of the class. It will initialize the virtual function table, and call base class constructors, but that is all - the attributes could be random garbage. For another example, without a copy constructor, the compiler will generate one that simply makes a bit wise copy of the attributes. If these attributes contain pointers, then you have two pointers to the same object, not necessarily a good thing, especially if one of them get "deleted".


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


When do you get a default constructor When the class has no other constructors When you define at least one constructor When you define a class None of the above?

hjuki


What happens if a class defines constructor but not one with no argument?

Nothing Happens. Actually such a constructor is called a Default Constructor. Even if we do not write a constructor for a class, Java would automatically place a default constructor inside the class. Ex: Public class Test { public String getName(){ return "Hi"; } } Public class TestEx { public static void main(String[] args){ Test obj = new Test(); System.out.println(obj.getName()); } } Here we were able to instantiate an object of class Test even though we did not define a constructor for that class. This is because Java automatically places a default constructor for the class.


Do you have to declare a constructor every time you create a class?

Yes, If you don't a default constructor will be created for you.


Is it mandatory to use the construtors in a class in c plus plus?

No. If you do not provide a default constructor, the compiler will provide a default constructor that simply allocates memory for the class, but it will not initialize the members of the class. If you do not provide a copy constructor, then the compiler will provide a copy constructor that allocates memory for the class, and then copies the member's data from class to class. This is bad if the class contains pointers, because only the pointer will be copied - the objects to which the pointers point will not be copied - and you could wind up deleting an object and then using it after deletion, with potentially devastating consequences. So, yes, it is mandatory, from a good practices point of view, and just plain mandatory when the class has pointers, to always provide a default constructor and a copy constructor, along with the appropriate destructor.


Using default constructor in inheritance and in what reason it throws error?

The compiler places a default no-arg constructor in any java class that does not have an explicit constructor coded into it. for ex: public class Car { ... ... //lots of code but no constructor } In the above case, the compiler will place the below constructor into the code: public Car() { super(); } But, if you have a constructor in your class that takes arguments then the compiler will not put the default constructor. Ex: public class Car { public Car(String name){ ... } ... //lots of code } Above, we have a Car constructor that takes a string name as argument. so, the compiler wont put the default constructor in the code. now, if you try to do: Car obj = new Car(); you will get an error because this constructor is not defined.