Broad Network


Using Java Objects

Java Object Oriented Programming Core – Part 6

Forward: In this part of the series I say more on how to use Java objects. In part 2 (Some Good Java OOP Features) of this series, I said certain things about using Java objects. In this part of the series I continue that talk, but this time, with the pertinent issues.

By: Chrysanthus Date Published: 3 Sep 2012

Introduction

This is part 6 of my series, Java Object Oriented Programming Core. In this part of the series I say more on how to use Java objects. In part 2 (Some Good Java OOP Features) of this series, I said certain things about using Java objects. In this part of the series I continue that talk, but this time, with the pertinent issues.

Initializing Properties
Initializing properties means giving the first values to the properties just after instantiation of the class. There are three basic ways of doing this: using constructor, at declaration or assignment after object instantiation.

The following shows the case of the use of a constructor (a constructor is a method that has the same name as the class):

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 Machine2
    {
        public static void main(String[] args)
            {
                Calculator obj1 = new Calculator(2,3);

                int result1 = obj1.add();

                System.out.println(result1);
            }
    }

The following example shows initialization at declaration (class description); there is no constructor there:

class Calculator
    {
        int num1 = 2;
        int num2 = 3;

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

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

                int result1 = obj1.add();

                System.out.println(result1);
            }
    }

The following is an example of assignment after object creation.

class Calculator
    {
        int num1;
        int num2;

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

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

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

                int result1 = obj1.add();

                System.out.println(result1);
            }
    }

For this example, the set method can be used to initially assign the values. For the three approaches, the constructor method is the official approach.

The static Reserved Word
The static reserved word is used to declare static properties and/or static method(s) of a class. A static property is a property whose value is the same for all objects already created and all objects still to be created and for the class itself. A static property can be used directly by the class without instantiation or normally by the objects after instantiation. A static property value can be changed by the class or by any of its objects. Once changed, the change is reflected in the class and all objects already existing and to come.

A static method is a method of a class that can be used without instantiation of the class. I discussed static properties and static methods in the tutorial titled, “Static Properties and Methods in Java” in the series, “Java Just After the Basics”. To reach the tutorial for details, click the link below titled, “Java Course”.

The static Modifier of the Main Method
The main method is in the main class. The main method has the name, main. However, the main class has the name of the program, which is a name of your choice. The skeleton of the main class is:

class MainClassname
    {
        public static void main(String[] args)
            {
                //statements
            }
    }

You can see the main method declaration. The parameter of the main method is, “String[] args”. I explained that in the first series of the volume. The reserved words, “public”, “static” and “void” preceding the main method declaration are examples of what is called modifiers. Our interest here is the static modifier. Execution of a program begins at the main method.

The main method is declared and used as a static method. It is the Java operating system, which is the Java Virtual Machine that calls (and uses) the main method, from where the main method calls other methods elsewhere in the program (in different classes). A static method is a method that can be called without instantiation of the class. So, the Java Virtual Machine calls (and uses) the main method without instantiation of the main class. Execution of a program begins in the main method. It is the main method that calls other methods in other classes of the program; that is how execution leaves the main method to come back to it at the end of the program. That is, after a call of another object method from the main method, execution returns to the main method at the statement just below the call of the other method. Execution continues like this until it reaches the end of the main method, which is the end of the program.

Modifier
A modifier is a reserved word that precedes a declaration. In the declaration,

    int myInt;

int is a modifier. Modifiers, such as “void” or “static” can also precede a method declaration.

Constant in Java
In the C++ language, there is a lot of emphasis on how to create a property with a constant value. In Java there is not much emphasis on that. However, you can create a property with a constant value in Java as I explain in this section. A property is said to have a constant value if the value cannot be changed elsewhere in the program.

Java has a modifier named, “final”. The final modifier indicates that the value of a property cannot change.

The mathematical constant, Pi is, 3.141592653589793. If you need such a constant in your program, a good way to declare it is as follows:

    static final double PI = 3.141592653589793;

where PI is the property name (variable). Here, "final" means the value for the property cannot be changed anywhere else in the program. Here, "static" means that a different class can read the value of the property, without instantiating the class that has the final property.

The following program shows how a constant can be used. There are two classes in the code. One class has only the constant, PI. Actually, PI is a constant in mathematics. It is used to calculate the circumference (distance round) of a circle when the radius is given. The radius is the distance from the center of the circle to any point on the curving line tracing the circle (circle line). To calculate the circumference, you multiply 2 by the value of PI and by the radius, That is:

    circumference = 2 X PI X Radius

Read and try the code:

class MyClass
    {
        static final double PI = 3.141592653589793;
    }

class Mathematics
    {
        double radius = 2.5;

        double circumference(double radius)
            {
                double circumf = 2 * MyClass.PI * radius;
                return circumf;
            }
    }

class Math
    {
        public static void main(String[] args)
            {
                Mathematics obj = new Mathematics();
                double answer = obj.circumference(3.5);
                System.out.println(answer);
            }
    }

Note: in programming calculation code, you do not type any unit (such as cm, m, km, etc.) next to a number. The printed answer can be rounded to two decimal places, but I will not go into that. Note how PI has been used in the two statements having it above.

The this Reserved Word in a Constructor
The "this" reserved word can be used in any class contructor during instantiation. Whether the "this" reserved word is used in a constructor or in an ordinary method, it is used to represent the object under execution (current object) in a method. However, in the Java constructor, since a constructor is used to initialize an object, the word has two purposes. You use it to call another constructor in the class declaration : you just type the word in the constructor followed by parentheses; in the parentheses, you type arguments for the other constructor. The other purpose is to use it as you would use in an ordinary method. The following program illustrates the first purpose:

class MyClass
    {
        int var1;
        int var2;
        int var3;
        int var4;

        MyClass()
            {
                this(10, 20, 30, 40);
            }
        MyClass(int ident1, int ident2, int ident3, int ident4)
            {
                this.var1 = ident1;
                this.var2 = ident2;
                this.var3 = ident3;
                this.var4 = ident4;
            }
    }

class ThisConstruct
    {
        public static void main(String[] args)
            {
                MyClass myObj = new MyClass();

                System.out.println(myObj.var1);
                System.out.println(myObj.var2);
                System.out.println(myObj.var3);
                System.out.println(myObj.var4);
            }
    }

Read and try the code. There are two constructors in the code. The first one takes no argument. The second one takes four arguments. The body of the first one has just one statement, which is "this" and its arguments. There is no dot after "this" to connect it to a property or method; that is not necessary because, as you should know, "this" can act as an object representative as well as a method call (that would receive arguments). Inside the parentheses of "this" you have arguments for the second constructor. The second constructor uses "this" in the normal way to initialize the properties of the object.

In your program, you can call the constructors independently. However, if you call the first constructor, it will simply call the second constructor to instantiate an object. The object then instantiated, would be as if the second constructor was called directly. This is just what has happened in the main method of the program.

Java Variables
There are three kinds of variables in Java. The names and meanings are:

Member variables - these are class properties.
Method or block variables - these are variables declared in a method block or in a block like the if-block. They are also called local variables.
Parameter Variables - These are variables declared as parameters in the parentheses of a method declaration (or signatue).

Method Returing a Value
This topic is so important that I have dedicated a whole part (tutorial) of the series to it: the next part. See you there.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message