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

  1. 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.

Related Searches:

References

  • Photo Credit "S-press-O" is Copyrighted by Flickr user: Demion under the Creative Commons Attribution license.

Comments

You May Also Like

  • Java Tutorial on the Random Method

    Randomly generated numbers have many uses in computer programs, such as creating unpredictability in games, modeling simulations and performing encryption. Java ...

  • Overloading Methods in Visual Basic

    Overloading Methods in Visual Basic. In object-oriented programming (OOP), programmers can create virtual copies of objects from schematics called classes. Classes ...

  • How to Write a Copy Constructor

    In C++, a copy constructor is a special method created within a class that returns an exact duplicate of the object that...

  • How to Use Copy Constructors in C++

    A copy constructor is a special member function inside a class. It accepts a reference to an existing object of the same...

  • Sun Java Tutorial

    Sun Java applications apply to a wide variety of contexts. Java programs built using the object oriented model are composed of distinct...

  • Get and Set Method in Java

    Java is an object-oriented programming (OOP) language. Object-oriented code is organized using a construct called a "class," which serves as a blueprint...

  • Java Applet Methods

    Java Applet Methods. Java Applets are a web-based method to package and deploy a Java program. Applets can be downloaded from the...

Related Ads

Featured