How to Call a Constructor From Another Function in Java
In Java programs, calling constructor methods allows your code to create object instances of classes. When you write a class declaration in Java, you specify one or more constructor methods. Code external to the class can call these methods to create objects that will have the properties and behavior outlined in the declaration. You can call a class constructor from anywhere else in your application, including from inside other class declarations and functions, which are called methods in Java. Depending on the class constructor, you may also need to pass parameters.
Instructions
-
-
1
Locate the point in your application code in which you want to create the object. This can be anywhere in your application, in the main class or another class. It can also be inside a method in any class. As long as the target class is accessible from where you are writing code, you can call the constructor method. In most cases, your Java classes will be inside a single application package, so the classes can all access one another.
-
2
Call the class constructor method. To call a constructor method, your code needs to use the class name and the "new" keyword. The following sample code demonstrates the technique:
new TaskHandler();This code applies to a class declaration named "TaskHandler" that is within another file in the application. The "new" keyword instructs Java to look for the constructor method inside the specified class declaration and execute it. When the constructor method executes, it returns an object instance of the class.
-
-
3
Store your new object in a variable. You can use objects of a class directly, on the same line as calling the constructor method. However, it is generally more useful to store the object instance in a variable reference. Extend your code as follows:
TaskHandler hand = new TaskHandler();This variable now holds a reference to the object instance. You can use the variable name to call methods outlined in the class declaration.
-
4
Pass parameters to the constructor method. Depending on the class you are trying to instantiate, you may need to pass parameters when you call the constructor method. The following sample code indicates the outline of a constructor method:
public TaskHandler(String name)To create a new object of this type, your code has to pass a string value or variable to this constructor method as in the following example:
TaskHandler hand = new TaskHandler("Manager");The following code is an alternative method for the same process:
String handlerName = "Manager";
TaskHandler hand = new TaskHandler(handlerName);Alter your code to suit the constructor method of the class you are using.
-
5
Use the object resulting from your constructor method call. You can use the class methods directly as follows:
new TaskHandler(handlerName).doSomething();This would apply to a method named "doSomething" that is listed inside the class. Alternatively, you can call methods on the object variable as follows:
hand.doSomething();Use your object instance to implement the functions in your application.
-
1
Tips & Warnings
Look at the class declaration or documentation for any Java resources you are planning to use before you attempt to instantiate them.
Make sure you understand what a class is for before calling its constructor method.