Broad Network


Java Class Constructor Method and Related Features

Java Object Oriented Programming Core – Part 3

Forward: In this part of the series, I treat officially, Java Class Constructor Method and its related features.

By: Chrysanthus Date Published: 3 Sep 2012

Introduction

This is part 3 of my series, Java Object Oriented Programming Core. In this part of the series, I treat officially, Java Class Constructor Method and its related features.

Meaning of Constructor
A constructor method, simply called, constructor, is what actually instantiates a class into an object. The constructor is described (written) in the class description. It is a method. It has the same name as the class. It does not have a return type, so it returns nothing. It does not begin with anything like void, int or float, etc., because it returns nothing. Its main function is to initialize some or all of the properties of the class, when it is instantiated into an object. When the class is an object (many objects of the same class are possible), the initialized property values belong to that object and not to the class. Different objects from the same class can have different initialized property values. Of course, a constructor can optionally have parameters and take arguments in the instantiation statement.

If you do not code a constructor, Java provides one for you, automatically, unknown to you. The constructor provided is called the default constructor.

Example with Default Constructor
In the following program, no constructor for the class, Calculator has been provided. So java provides one.

class Calculator
    {
        int num1;
        int num2;

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    }

class Construction
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator();

                obj1.num1 = 2;
                obj1.num2 = 3;

                int result1 = obj1.add();

                System.out.println(result1);
            }
    }

Read and try the code. There is no constructor description in the Calculator class description. So, Java provides a Default one for you. The default constructor syntax that Java provides for you is:

    ClassName()

It takes no argument. To instantiate a class from such a constructor, you use the syntax,

    new ClassName()

Here, new is an operator. The return value is assign to a declared class variable, as in:

    Calculator obj1 = new Calculator();

So, the default constructor method has the class name (and parentheses) but takes no argument. The default constructor also does no initialization.

Programmer Constructor
You the coder (programmer) can provide your own constructor, which basically initializes some or all of the properties of the object created. The default constructor does not initialize the properties of the class. A constructor that initializes properties of a class, can take arguments. The arguments are the values, used to initialize the properties. Read and try the following code:

class Calculator
    {
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {//constructor method
                num1 = ident1;
                num2 = ident2;
            }

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    }

class Construction2
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator(2,3);

                int result1 = obj1.add();

                System.out.println(result1);
            }
    }

The property variables, num1 and num2, declared outside the constructor method, are seen inside the block of the constructor method.

Note. It is not all classes that need constructor method from a program.

The Word, new
The word, new, used to instantiate an object, means new object. In the above cases, it means, new Calculator. Remember, it is the object that works and not the class (for most programs). The word, new, is an operator.

The Garbage Collector
With a language like C++, after using certain instantiated objects, you have to destroy them before the program ends. If you do not do that in your code, the objects will stay in memory and occupy memory unnecessarily. In Java, the programmer does not need to destroy any object. The Java Virtual Machine (Java operating system) does that for you. So, in a Java program, the programmer does not have to use any destructor code to destroy any instantiated object. That should be good news.

Assigning Instantiated Objects
Assume that you have an object declared variable of a class, not yet instantiated. Assume that you have an instantiated object of the same class. The variable of the object, not yet instantiated, can receive a copy of the instantiated object, just by using the assignment operator. Remember, both of them are of the same class. The following program illustrates this:

class Calculator
    {
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {//constructor method
                num1 = ident1;
                num2 = ident2;
            }

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    }

class ObjCopy
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator(3,4);
                Calculator obj2;
                obj2 = obj1;

                int result2 = obj2.add();

                System.out.println(result2);
            }
    }

The line that does the copy is:

    obj2 = obj1;

Read and try the above code if you have not already done so. The variable, obj2 is declared, of the class Calculator, and not yet instantiated. The variable, obj1 is also declared of the class Calculator and is instantiated. The assignment operator copies the content of obj1 to obj2. This kind of copying, indirectly instantiates the previously un-instantiated object. The last statement of main displays the addition result of the values within obj2, which is a copy.

Assume that you have two instantiated objects of the same class. If you copy one to the other, the one receiving the copy looses its own content. The following program illustrates this:

class Calculator
    {
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {//constructor method
                num1 = ident1;
                num2 = ident2;
            }

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    }

class ObjCopy2
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator(3,4);
                Calculator obj2 = new Calculator(5,6);
                int result2 = obj2.add();
                System.out.println(result2);

                obj2 = obj1;
                result2 = obj2.add();
                System.out.println(result2);
            }
    }

Read and try the above code. In main, the first print statement displays the result of obj2, which is 11. After the copy of the content of obj1 to obj2, the result of obj2 becomes 7, confirming that all its content has been replaced.

Deleting Object Content
If you want to delete the content of an object, just assign its declared variable to the constant, null. Read and try the following program that illustrates this:

class Calculator
    {
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {//constructor method
                num1 = ident1;
                num2 = ident2;
            }

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
    }

class DeleteObjContent
    {
        public static void main(String[] args)
            {
                Calculator obj = new Calculator(3,4);
                int result = obj.add();
                System.out.println(result);

                obj = null;
                //result = obj.add();
            }
    }

The last statement, which is actually a comment is not executed. If you remove the comment sign in front of the statement, the statement will generate an error, because null has been assigned to obj. Assigning null is not the problem. Using the null assigned variable as a complete object, is the problem.

Copy Method
Hey, you can code a method that will copy the content of one object to another, of the same class. When you want a copy, just call the copy method. Read and try the following program that illustrates this:

class Calculator
    {
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {//constructor method
                num1 = ident1;
                num2 = ident2;
            }

        int add()
            {
                int sum = num1 + num2;
                return sum;
            }
        Calculator copy(Calculator objec)
            {
                return objec;
            }
    }

class CopyMeth
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator(3,4);

                Calculator obj2;
                obj2 = obj1.copy(obj1);
                int result2 = obj2.add();
                System.out.println(result2);
            }
    }

The copy() method can copy any object of the same class and not just the object that it belongs to. For example, you can have something like this:

                obj2 = obj1.copy(obj3);

Copying Objects of Different Classes
You can code a copy method that will copy an object of one class to replace the content of another object of a different class. You will need a method more complicated than the one above which takes account of the differences. However, I will not go into that in this volume.

Purpose of a Class
When you design a class, you should write your name, date of class design (coding) and the purpose of the class. Under purpose, you can optionally give a brief explanation of what goes on in the class.

The following code skeleton shows how to do this:

class ClassName
    {
        //Author:
        //Date:
        /**Purpose and possible explanation*/
    
        int property1;
        int property2;

        void method1()
            {
            }
        void method2()
            {
            }
    }

The copy-right like information is in comments.

That is it for this part of the series. Let us take a break and continue in the next part.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message