answersLogoWhite

0

Need of constructors in java

Updated: 8/18/2019
User Avatar

Wiki User

12y ago

Best Answer

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

Add your answer:

Earn +20 pts
Q: Need of constructors in java
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

For what purpose constructors are used in Java?

Constructors are used to create the instance of a class.


What is copy constructor in java?

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


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


When constructor function are called in java?

Constructors are called during object creation.


Can you instantiate an interface?

According to a beginner's book on Java, an interface can't have constructors. Also, the interface itself can't contain the method implementation.


What do you mean by this keyword in java?

"this" is a Java keyword that references the current object. Any part of the object(instance variables, methods, constructors) can be accessed by calling this.[member].


Which is the most usable method or constructor in java?

There is no comparison between methods and constructors. They are both present for a reason and each has its own purpose.


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.


Can you have more than 1 constructor in a class?

Yes. At least in Java, that's possible, as long as the constructors have a different number, or different types of, parameters.


What is overloading constructor in java?

This is when you overload the constructor so that there isn't just one way you can initialize the class objectAssume you have a class called Foo and you want two different constructorsFoo(int a, int b);Foo(float a, float b);using a method like this can make your classes more dynamic and you don't need to to write a new class to handle different types. Sorry for the lack of Java code, i program in C++ but it you can overload functions and constructors in C++ too.


What are special properies of constructor in java class?

Two key points to remember about constructors are that they have no return type and their names must exactly match the class name


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.