answersLogoWhite

0

How do you invoke a constructor in java?

Updated: 8/17/2019
User Avatar

Wiki User

12y ago

Best Answer

Constructor Basics

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?

User Avatar

Wiki User

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

Wiki User

12y ago

You can invoke the constructor of a class by using the NEW keyword

Ex: ClassName obj = new ClassName();

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you invoke a constructor in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the function of this in java?

this in java is a keyword that refers to the current object of the class. It is also used in constructor overloading when you want to invoke one constructor from another within the same class.


What is the uses of constructor in java?

The constructor will invoke all constructors in the inheritance hierarchy to ensure that all the parent classes of the current classes get initialized when the current class is instantiated.


How do explicitly invoke superclass constructor from a subclass?

Using the super keyword. If you call super() from within your constructor, it will explicitly invoke the superclass version of the constructor.


Does Java support copy constructor?

No. Java does not support copy constructor


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.


What is dynamic constructor?

In Java classes we can declare multiple constructors. The JVM would dynamically decide which constructor to invoke based on the parameters passed from the calling class. Ex: public class Test { public Test(){ ... } public Test(String arg1){ ... } public Test(String arg1, int arg2){ ... } } In the above class, there are 3 different constructor declarations. Whenever a constructor is invoked from a calling class, the JVM would decide which one to invoke based on the number of arguments passed.


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.