Constructors in Java

Walden Systems Geeks Corner Constructors in Java programming how to tutorial Rutherford NJ New Jersey NYC New York North Bergen County
Java Powers the Biggest Technology. Mobile. Web apps. The Internet of Things. Big data. Machine learning. Cloud platform and cloud infrastructure. The next big things are here now, and Java is at the center of them all. Whether you’re developing the robust enterprise back end, building the agile front end, or thriving in a DevOps role, Java skills can up your game. With more than 10 million programmers and 13 billion Java-enabled devices worldwide, Java is the language that connects applications to data, data to people, and people to their digital lifestyle.

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. We use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up procedures required to create a fully formed object. All classes have constructors, whether we define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used. Constructor method of a class has the same name as that of the class, they are called or invoked when an object of a class is created and can't be called explicitly.

Defining a constructor

The first part of a Java constructor declaration is an access modifier. The access modifier have the same meanings as for methods and fields. They determine what classes can access the constructor. Access modifiers are covered in more detail in my Java access modifiers tutorial. The second part of a Java constructor declaration is the name of the class the constructor belongs to. Using the class name for the constructor signals to the Java compiler that this is a constructor. The third part of a Java constructor declaration is a list of parameters the constructor can take. The constructor parameters are declared inside the parentheses after the class name part of the constructor . The fourth part of a Java constructor declaration is the body of the constructor. The body of the constructor is defined inside the curly brackets { } after the parameter list.

1    public class MyClass 
2    {
3
4        public MyClass() 
5        {
6    
7        }
8    }


Lines 4 - 7 is the constructor.


Multiple constructors for a Java class

A class can have multiple constructors, as long as their signature are not the same. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded. This is what constructor overloading means, that a Java class contains multiple constructors.

1    public class MyClass 
2    {
3
4        private int number = 0;
5
6        public MyClass() 
7        {
8        }
9
10       public MyClass(int theNumber) 
11       {
12           number = theNumber;
13       }
14   }


The Java class example contains two constructors. The first constructor takes no parameters and the second constructor takes an int parameter.

Calling a Constructor

We call a constructor when you create a new instance of the class using the new keyword. If we want to pass parameters to the constructor, we include the parameters between the parentheses after the class name. In Java it is possible to call a constructor from another constructor. When we call a constructor from inside another constructor, we use the this keyword to refer to the constructor.