What Is a Constructor Method in Java?
Java is an object-oriented programming language used to create applications in one environment that can run in another environment without needing to be recompiled. Because it is object oriented, Java uses a particular kind of function (aka _method_) that's called a "constructor." The constructor method creates a virtual object from its blueprint, which is called a"class."
-
Example Constructors
-
This example function creates an object by calling its constructor:
public class make_people {
public static void main ( String[] args ) {Worker obWorker = new Worker("Gus");
Plumber obPlumber = new Plumber("Shirley");
Plumber obPlumber2 = new Plumber(5);
Plumber obPlumber3 = new Plumber();}
}//end of make_people
-
Constructor Names
-
Two different kinds of virtual people are created in this example. One is a Worker; the other, a Plumber. The first Worker to be created, "Gus," is created with its constructor Worker([worker's name]). Notice that the constructor has the same name (and is spelled with the same case) as the name of the class, Worker. This is always the case with constructor functions: They always have the name of the class they are constructing an object for.
Notice also the new keyword. This is needed to create the new object. If this program were to declare an object variable with a statement like "Worker someWorker;" and the statement didn't use the new keyword, then no object is created.
Overloading Constructors
-
Constructors are typically overloaded, which means there can be several constructors for a class. However, each constructor must be different from the other, in terms of number of arguments they take, or the types of the arguments. In other words, this would produce a compile error, if it appeared in a class file:
Worker(int a, int b, int c);
Worker(int d, int e, int f);The sample program in this article shows several examples of overloaded functions being called. Three different Plumbers are created, each with a different constructor. One constructor takes a String as an argument; one takes an int(eger). One takes no arguments.
The Definition of a Constructor
-
What actually goes on inside a contractor is the initialization of member variables. Here's the Worker class initializing the "name" variable in its constructors:
public class Worker {
private String name;
public Worker () {
name = "<unnamed worker>";
System.out.println("I'm an unnamed Worker.");
}
public Worker(String argName) {
name = argName;
System.out.format("My name is %s\n", name);
}
};// end class Worker
Access Specifiers
-
Notice the _access specifier_ here: That's the "public" keyword that comes before Worker, in the constructor definitions. The "public" access specifier says that code outside the Worker class can call the Worker constructors. But if one of the constructors has the "public" changed to "private," code outside the Worker class cannot call that constructor.
References
- Photo Credit "S-press-O" is Copyrighted by Flickr user: Demion under the Creative Commons Attribution license.