answersLogoWhite

0

What is the constructors in class?

Updated: 8/10/2023
User Avatar

Rahulsawant

Lvl 1
12y ago

Best Answer

A class is simply the definition of a data type. A constructor is a special method of a class that is automatically invoked whenever an object of that type is instantiated, and is used to initialise the object's data members.

User Avatar

Wiki User

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

Wiki User

12y ago

The constructors in a java class are the methods set up to instantiate, or create an instance of the class. Often, a constructor will accept parameters to add information to the object that will be created. Here is an example:

public class Person

{

String name;

int age;

char gender;

public Person(String n, int a, char g)

{

this.name=n;

this.age=a;

this.gender=g;

}

public Person(String n)

{

this.name=n;

this.age=18;

this.gender='m';

}

}

In this example, the two methods in boldface are constructors. In another program they could be called, and an instance of the class would be created. The reason there can be more than one constrcutor( and i could have made only one) is that they take different parameters. The first one creates an object of type Person with 3 variables: age, name and gender. These three variables are all set by the constructor. The second constructor allows an instance to be created with only the name known. It sets the name to whatever value was specified as a parameter, and it sets the age to the default value of 18 and the gender to 'm'. Another program could look like this:

public class MakeAName

{

public static void main(String[] args)

{

Person joelle = new Person("Joelle", 15, 'f');

Person Tim = new Person("Tim");

}

}

This program, if it was in the same folder as the Person class file, would create two objects (variables). the first would be joelle, which would be an object of type Person with name "Joelle", age 15, and gender 'f.' The second would be Tim, which would be an object of type Person, with name "Tim". It would have age 25, and gender 'm', because in the constructor i only listed a String value ("Tim"), but no int or char values. therefore, the program would know to run the constructor that accepted only a String value (the second constructor). This constructor set the age to 25 and the gender to 'm'. remember? anyway, another thing is that you cannot call a constructor with a different number or type of variables than already specified. So I could not have said in the second program :

Person Jim = new Person("Jim", 34).

This is because i never made a constructor that was declared as such:

public Person(String s, int i)

{

//put code here

}

And my final point i would like to say is that, if a class (that is not a static class, which means it cannot have a constructor, but this is more advanced stuff) does not contain a constructor at all, it can still be instantiated. For example, if there was a class called Animal, but it did not have a constructor in it, it could still be instantiated as such:

Animal a = new Animal();

This would create the Object (or variable) a which has the type Animal. However, it does NOT contain any personalized data as the Person class did (name, age, and gender)

So that is what a constructor is. Hope it helped!!!!

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Class constructor is a structure within class definition that serves to create and/or initialize variable defined within class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the constructors in class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you identify a constructor from among any other methods in a class?

Constructors have the same identifier as that of the class, so if the name of your class is Book then your constructor must also be named Book. Constructors have no return type, not even void.


How many constructors does class Exception have?

The Exception class has 4 constructors. They are: a. Exception() b. Exception(String arg) c. Exception(String arg, Throwable arg1) d. Exception(Throwable arg)


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 many constructors can c have?

A class can have any number of constructors, as far as they are having different parameters or different number of parameters. For example, a class A can have following constructors & even more: A() -the default constructor A(A objectA) -the copy constructor A(int p) A(int p1, int p2) A(int[] p1, float p2) A(double p1, double p2, int p3) A(A objA, int[] p) A(B objB)


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

Related questions

For what purpose constructors are used in Java?

Constructors are used to create the instance of a class.


How can you identify a constructor from among any other methods in a class?

Constructors have the same identifier as that of the class, so if the name of your class is Book then your constructor must also be named Book. Constructors have no return type, not even void.


What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors


Why you use constructors?

To create an instance of the class that implementing that constructor


What is main advantage of using constructors in java?

Constructors have the same name as the class itself and they do not specify a return type, not even void because they return the instance of the class itself. Because constructors have the same name as the class then they allow method overloading and also save memory and execution time of program. Program release memory of constructors function after using this function and it reduce program complexity.


How many constructors does class Exception have?

The Exception class has 4 constructors. They are: a. Exception() b. Exception(String arg) c. Exception(String arg, Throwable arg1) d. Exception(Throwable arg)


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 many constructors can c have?

A class can have any number of constructors, as far as they are having different parameters or different number of parameters. For example, a class A can have following constructors & even more: A() -the default constructor A(A objectA) -the copy constructor A(int p) A(int p1, int p2) A(int[] p1, float p2) A(double p1, double p2, int p3) A(A objA, int[] p) A(B objB)


What is different between constructor and method?

Constructors have no return type and their names must exactly match the class name. Apart from this constructors and methods are similar to one another.


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 is a constructor with value zero?

Constructors have no value, zero or otherwise. That is, constructors cannot return a value. This is because constructors are not functions in the sense you cannot call a constructor directly. Constructors are invoked in the background when you instantiate an object of the class, thus any return value would be lost in the background, and would therefore not be visible to the invokee.


Why constructors are declared public only?

Constructors are not declared public only. They can be declared protected and private, as required by the class. Protected constructors are only accessible to the class members and to derived classes. Private constructors are only accessible to the class members. Although a default public constructor is required by the majority of classes, it is not true of all classes. Derived classes can call any public or protected base class constructor explicitly via their own construction initialisation sections. Private construction is typically used in the singleton model to instantiate a private static instance of the class as and when it is required, whilst preventing multiple instances of the class from being created. However, class members also include friends of the class, thus external friend classes and friend functions can also instantiate objects via their private constructors. Note that the whole point of public, protected and private access is not to hide information (as is often wrongly said) but in order to limit access to that information. The same applies to a class' constructors as it does to its member methods and member variables.