answersLogoWhite

0

Why constructor is used?

Updated: 12/16/2022
User Avatar

Wiki User

10y ago

Best Answer

Objects are constructed. You can't make a new object without invoking a constructor. In fact, you can't make a new object without invoking not just the constructor of the object's actual class type, but also the constructor of each of its superclasses including the Object class itself! Constructors are the code that runs whenever you use the keyword new. The constructor typically contains he initialization code that you want to run when someone is instantiating an object of a class that you are coding.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why constructor is used?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


Which situation constructor is used?

Constructor is necessary when you are about to use instance of a class.


What is concession constructor?

Constructors are basically used to evoke methods of a class creating its object..and as far as i know there is no constructor called concession constructor..


What is conustrutor?

A constructor is a function in C which has the same name of the class. The constructor can be used to initialize some function.


Static can be used in front of a constructor?

No. Static elements belong to the class, and the constructor belongs to the object (which is an instance of a class).


Can you define the constructor as private?

Yes, you can. Making a constructor private ensures that no other class can instantiate that class you just created with a private constructor. It is usually used in Singleton Patterns.


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


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.


What is an empty constructor?

An empty constructor takes no arguments and calls the default constructor


What does it mean in java when a constructor is undefined?

When a constructor is not define in java then the instance used in class is not optimised the value and therefore some times it generates some garbage value. By the way , When we not define a constructor then generally it not distrub the execution of the program.