answersLogoWhite

0

What is a constructor method?

Updated: 8/9/2023
User Avatar

Wiki User

13y ago

Best Answer

Constructors and the main method serve two different purposes. Constructors allow creation of instances of a given Class, whereas the main method merely allows for a potential entry point for starting your program.

To learn more about data science please visit- Learnbay.co

User Avatar

Learn bay

Lvl 8
1y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this:

class Car {

Car() { } // The constructor for the Car class

}

You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile:

Car f = new Car(); // Won't compile, no matching constructor

but the following will compile:

Car f = new Car("Ford", 43); // No problem. Arguments match

// the Car constructor.

So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods). You can't always make that work for your classes; occasionally you have a class where it makes no sense to create an instance without supplying information to the constructor. A java.awt.Color object, for example, can't be created by calling a no-arg constructor, because that would be like saying to the JVM, "Make me a new Color object, and I really don't care what color it is...." Do you seriously want the JVM making your color choices?

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The main method is the method called when the Java application is started

The constructor is called whenever a new object is instantiated.

This answer is:
User Avatar

Add your answer:

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

Is constructor overloading same as method overloading.yes or no. explain?

yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.


What is first call in struts2 default constructor or validate method?

Default Constructor will be called first . If you override Validate method , then validate method will be called .


Can a constructor be defined within a method in java?

No.


What is a no args constructor?

No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.No args means no arguments. Just like any regular method, a constructor can have zero or more arguments.


Differene betwee constructor overloading same as method overloading?

A constructor is just a special form of a method. You can overload constructors in the exact same way as you can overload any other method.


What is the diffferent between method and constructor?

Both are functions, i.e., places where you can write code. A constructor is simply a special method that is invoked automatically when an object is created.


A subclass can call constructor method defined by its super class?

True


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.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


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.


How do you call an abstract class from main method?

An abstract class cannot have a constructor and hence you cannot invoke the constructor of the class - i.e., you can instantiate an abstract class and hence you cannot call the constructor of an abstract class.


Why you don't use void in constructor function although it doesn't return any value?

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this: MyClass var = new MyClass(); Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler. public class MyClass{ // The java compiler will insert a real constructor here public Integer MyClass(){ //This isn't a constructor, only a simple method return new Integer(1); } }