answersLogoWhite

0

When to use a constructor in java?

Updated: 8/10/2023
User Avatar

Wiki User

14y ago

Best Answer

Private constructors generally have one of two uses.

The first is for implementing the singleton design pattern, in which you only ever want one instance of the class in existence. So you don't want new instances of the class being generated over and over. You do something like:

class Singleton {

// All classes can access this instance of the class and no other.

public static instance = new Singleton();

private Singleton() {

}

}

The second is for limiting the creation of classes which contain only static methods or constants.

Of course, private constructors are useful any time you want to limit the way other classes/packages/programmers interact with your class.

User Avatar

Wiki User

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

Wiki User

14y ago

You use constructors on a class for instantiation of an object.

For example:

You have a

class Person()

private String name;

private String address;

private int number;

public Person(String name, String address, int number)

{

/*lines of codes*/

}

in the code you need two Persons object.

Person person1 = new Person("mark","USA", 23)

Person person2 = new Person("anne","Japan" 56)

person1 is a new object instance

person2 is another new object instance which has independent state from person1

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

What are the importance of constructor in java?

Constructor is automatically called immediately after the object is created , before the new operator complete . Constructor is important because they have no return type , not even void.

This answer is:
User Avatar

User Avatar

Wiki User

12y 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

You will use Private constructors when you dont want any other class to instantiate your current class using a new keyword.

ex:

Ferrari obj = new Ferrari();

The above line of code will not work if the constructor of the Ferrari class is declared private

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

No. The constructor is the starting point of creation of any object in Java. Without going through a constructor you cannot create an object

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A Private constructor is simply a constructor that can only be used within the class it was declared in. By default java creates you a constructor which is public though. Hope that helps

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

We use constructor to initialize the instant variable of a particular class that is used for the various operations.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes, that's how create SINGleton

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When to use a constructor in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is use of constructor in java?

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


Why constructor use in java?

To create objects of classes


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


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.


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.


Can you use constructor in servlet?

Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them. Note: Even if you write a constructor in a servlet, it will not get executed.


Why Java always provides a default constructor to class when you use another constructor default constructor remove?

Classes in Java inherit constructors from their parent classes. If you don't explicitly define a parent class, then Object is used, which has only the default empty constructor. That "default" constructor is only there when defined by the parent class, so classes which do not have a no-argument constructor will not allow subclasses to automatically use it. This is implemented this way because of the special nature of constructors. Java could not always provide a default constructor because it could not guarantee that all class members would be properly created or initialized.


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.