Java ProgrammingLearn more about Object oriented programming in Java

Learn more about Object oriented programming in Java

When you create a class, you are said to encapsulate functionality inside that class. That is, any object that is instantiated from this class will have access to properties (attributes), and methods (functions). Attributes are used to describe the object. For example, a student object has a first_name, last_name, and grade attributes. Methods are used to define actions that this object is capable of. For example, our student object may have a register() method.

Declaring and calling instance fields

An object properties/ attributes are called instance fields. Each object maintains a local copy of it’s own instance fields. Multiple objects can have multiples fields of the same name, each having a different value. You declare an instance field the same way you declare a variable. For example:

public class Student {
    int age = 18;
    String first_name = "Jack";
    String last_name = "Black";
}

As you can see, you can optionally initialize the fields before using them. If you don’t, fields will be initialized to the default value, which is 0 for numerical types (int, float, double…etc.) and an empty string for textual ones.

Accessing the instance field from inside the class definition is as simple as calling it, while doing so from outside the object requires that you prefix the field with the object name. Have a look at the following:

public static class Student {
        int age = 18;
        String first_name = "Jack";
        String last_name = "Black";
        public Student(){
            System.out.println(first_name);
        }
    }
public static void main(String[] args) {
	Student myStudent = new Student();
        System.out.println(myStudent.first_name);
    }

Notice the way we are calling the first_name instance field from inside the constructor function, and how we had to prefix it with myStudent when calling it from the main() function, outside class declaration.

In general, it is a good practice not to initialize instance fields upon creation like we did in the above example, however, to assign value to them in the constructor function of the class. For example:

 public static class Student {
        int age;
        String first_name;
        String last_name;
        
	public Student(String _first_name, String _last_name, int _age){
            first_name = _first_name;
            last_name = _last_name;
            age = _age;
        }
	
	public Student(){
        }

    }
    public static void main(String[] args) {
        // TODO code application logic here
        Student myStudent = new Student("Jack","Black",19);
        System.out.println(myStudent.first_name);
    }

In this example, the constructor function accepts 3 parametrs: _first_name, _last_name, and _age. Then, inside this function, we assigned those parameter values their respective field instances. We also have another constructor function that does not force the user to supply the student parameters on object creation, instead, they may be supplied later in the code.

Notice how we used the underscore _ character to differentiate the parameter names passed to the function from the instance field names. You can also use the keyword this to make the compiler refer to the instance field explicitly and avoid any confusion. Consider the following modified version of the above example:

public static class Student {
        int age;
        String first_name;
        String last_name;
        public Student(String first_name, String last_name, int age){
            this.first_name = first_name;
            this.last_name = last_name;
            this.age = age;
        }
        public Student(){
            
        }
    }
public static void main(String[] args) {
        // TODO code application logic here
        Student myStudent = new Student("Jack","Black",19);
        System.out.println(myStudent.first_name);
    }

Using the keyword this means that you are going to use the instance field of the class, and, thus, you can use the same names for your parameters without worrying about collisions.

Using methods

As mentioned before, methods are ways to describe a class behavior. They are created and used the same way functions are. Check out the following example:

public static class Student {
        int age;
        String first_name;
        String last_name;
        
        public Student(){
            
        }
        
        public static void register(){
            System.out.println("The student is now registered");
        }
    }
public static void main(String[] args) {
        Student myStudent = new Student();
        myStudent.register();
    }

Here we defined a method called register(). It should be called, for example, when the student pays the tuition fees to register himself/herself to a course. This method becomes immediately available to all Student objects.

A method can have a return type. In this case, it should be initialized as follows:

public static boolean register()

But in this case, you must add a return statement at the end of the method body with a matching return type, Boolean in our case. If you do not intend to return a value from the method, you must add the keyword void before it’s name in the declaration. You can also instruct it to accept parameters like the following:

public static boolean register(String course)

It is worth noting that you can use the return statement even if the type of the method is void. In this case, the return statement will immediately terminate the function and returns control to the calling code. Take a look at this:

public static class Student {
        int age;
        String first_name;
        String last_name;
        
        public Student(){
            
        }
        
        public static void register(String course){
            if (course == "Information Systems"){
                return;
            } 
            else {
                System.out.println("The student is now registered");
            }
        }
    }
    public static void main(String[] args) {
        Student myStudent = new Student();
        myStudent.register("Computer Science");
    }

In this example, if the method gets called with a parameter “Information Systems”, it terminates and code execution continues past the method call. Otherwise, the method moves to the next statement.

Java Programming Course for Beginner From Scratch

Method chaining

Lots of times you’ll encounter the following form of code in Java:

myStuedent = new Student().register().enroll();

Such a code is an example of method chaining. It’s a more compact way of dealing with object methods, as long as they work on the same object. To achieve this, you must declare the method return type as the object itself, and return a reference to the object. An example will make things clearer:

public class Student {
    int age;
    String first_name;
    String last_name;
    
    public Student register(){
        System.out.println("Registerd successfully");
        return this;
    }
        
    public Student enroll(){
        System.out.println("Enrolled successfully");
        return this;
    }
}

This is the student class. We moved it to a separate file Student.java to be able to call it without the static keyword (more on that later). Notice that the two methods register() and enroll() have the Student type. Accordingly, when they return their value, it must be of type Student. So that are returning this, which is a reference to the current object, Student. This way you can instantiate the object, and call the register() and enroll() functions on it, all in one step:

public static void main(String[] args) {
        Student myStudent = new Student().register().enroll();
    }

Passing an arbitrary number of objects to the method

You can pass parameters to a given method that matches the number and type of the parameters declared in it’s definition. But what if you can’t determine the exact number of parameters to pass? For example, let’s say you have a method that calculates the total of the parameters passes to it. But the number of parameters to pass is unknown to you, and it may vary from one method call to another. In this case, you can use varargs. Take a look at the following example:

[Student.java file]
    public int sumGrades(int...grades){
        int sum = 0;
        for (int i = 0; i < grades.length; i++){
            sum = sum + grades[i];
        }
        return sum;
    }
[Main.java file]
    public static void main(String[] args) {
        Student myStudent = new Student();
        System.out.println(myStudent.sumGrades(20,50));
        System.out.println(myStudent.sumGrades(20,50,30,60));
    }

The output of this code will be:

70
60

Let’s how this happened. The only change we have here is that we added three periods between the parameter type and the it’s name. This way, you can add any number of integer values to the method when calling it, and the method will sum them up for you. Notice that when a method gets called this way, the parameter name is converted to an array, on which you can iterate and do any processing needed on the values within.

Conclusion

In this article we continued our discussion of object oriented programming in Java. We examined instance fields, and instance methods. We saw how to define and access each of them. Then we discussed method chaining as a way to call object methods in a more compact way. Finally, you learned how to pass a random number of parameters to a method.

In the next articles, we delve into more aspects of methods in Java, like recursion and overloading. See you next.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -