How to Force the Constructor for Non-Generic Use Only in Java
Each time you create a class in Java, a generic constructor is set up by the designer. The generic compiler does not take any parameters when you call the class. To force a user to use a non-generic class constructor, you change the generic constructor to require parameters. This means the user must pass parameters to the class constructor before assigning the class to a variable.
Instructions
-
-
1
Right-click the Java class file that contains the constructor you want to edit. Click "Open With," then double-click the Java editor you want to use to edit the constructor.
-
2
Add parameters to the constructor. The constructor is the first line of code in a class file. The constructor has the same name as the class itself. You can have one or several constructors in a file, but the generic constructor is the first constructor that defines no parameters. The following code is an example of the "customer" class constructor with added parameters:
public customer(string fname, string lname) {}
-
-
3
Click the "Save" button to save the changes to the constructor.
-
1
Tips & Warnings
Any class calls to the old, generic constructor must be changed to the new non-generic constructor.