answersLogoWhite

0

What is mean constructor in java?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

A parameterized constructor in java is just a constructor which take some kind of parameter (variable) when is invoked. For example.

class MyClass {

//this is a normal constructor

public MyClass(){

//do something

}

//this is a parameterized constructor

public MyClass(int var){

//do something

}

//this is another parameterized constructor

public MyClass(String var, Integer var2){

//do something

}

}

User Avatar

Wiki User

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

Wiki User

13y ago

It is also called the default constructor. It is the constructor that the java compiler places in every class where the programmer has not explicitly coded a constructor.

Ex:

public class Car {

...

//lots of code but no constructor

}

If a person writes a class as above, then the compiler places this code in the compiled version of the file.

public Car() {

super();

}

This is called the non-parameterized or default constructor

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A constructor is a method that is called whenever a class is created. It does the business of preparing the class (itself) so that it may do its job when called upon. It may set internal variables according to configuration information, it may create child objects with which it must interact, it may establish a remote connection, etc. In short, it does what needs to be done before the program may interact with it.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is mean constructor in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does Java support copy constructor?

No. Java does not support copy 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.


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( ) ").


Is there a sample program for constructor?

All Java programs would have a constructor... public class Test { public Test(){ ... } ..... } This is a constructor. Even if you dont code the constructor Java would automatically place a default constructor for compilation.


What is use of constructor in java?

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


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


Can a constructor be defined within a method in java?

No.


What is copy constructor in java?

Java, unlike C++ does not support copy constructors.


Can you create a constructor for an interface in java?

NO, we cannot create a contructor for an interface in java.


Why constructor rather than classes in java?

Constructor is not an alternative to class. In Java, you create classes; the classes contain methods - including the constructor, which can be viewed as a special method. If you want to have a constructor, you need a class that surrounds it, so it's not one or the other.


How many kinds of constructor in java?

Java has 2 types of constructors based on parameters passed:Default or parameter-less constructor: A constructor which does not accept any arguments.Parametrized constructor: A constructor which accepts one or more arguments.Similarly based on Access modifier also we have:Public constructor - Class can be instantiated by anyonePrivate constructor - Class cannot be instantiated by anyoneProtected constructor - Class can be instantiated only by sub classes


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.