Software Engineering Journey: Implementing Methods

I reflected on how object-oriented design can be translated to skeleton code in my previous post. I concluded my discourse with the idea that once skeleton code is generated, the only problem we are left with is to provide implementations of the bodies of the various methods of classes in the code. In this article, I shall be writing about this. My particular vantage point is to speak in terms of Java.

The first thing that we need to understand is that how methods receive and return inputs and outputs (IO). This is very important. The working of a method can be broadly divided into two types of tasks. One is to handle IO. The other is to manipulate inputs to generate outputs. This article particularly reflects on the first part i.e. how a method deals with IO.

To understand this, it will be very nice to remind ourselves that in Java all arguments to a method are passed by value. What this literally means is that if we have a variable ‘x’ to which we have assigned a value of ‘6’, and if we pass this variable as an input argument to some method (e.g. insertX(x)), then its value (6) is passed to the method call insertX(x);.

This is true about all sorts of method calls in Java, and all sorts of data types including primitive and abstract. But we need to understand a few more things about abstract or user-defined data types.

Consider the following variable declaration in Java:

int x=5;

The above declaration creates a variable of type int and assigns it a value of 5. x goes on the stack.

Now consider the following variable declaration:

MyDataType data;

The above declaration creates a variable of type MyDataType, which is essentially a user-defined data type. What is ‘data’? Well, data is a variable. Where is it located? It is located on the stack. What is its value? It hasn’t been assigned any value yet. How to assign a value to it? Following is the procedure for that.

data= new MyDataType();

What we have done above essentially is that we have created an object of type MyDataType (on the right-hand side of the expression). This object is allocated memory on heap since this is dynamic memory allocation (all dynamic memory allocation is done on the heap). The object does not have any name as such. However, it does have an address. And its address gets assigned to ‘data’ (left-hand side of the expression).

So the value of ‘data’ now is the address of the newly created object of type MyDataType.

Now, consider a method that accepts an object of type ‘MyDataType’. Let us consider the following method, for instance.

public class Example{
//
//

public MyDataType insertData(MyDataType data){
//
//
return data;
}
}

The method ‘insertData’ accepts an argument of type ‘MyDataType’. It also returns an argument of type MyDataType.

Now consider the following method call.

MyDataType data2=insertData(data);

where data is the variable mentioned and explained two paras above. Since Java is pass-by-value,  when we pass ‘data’ to ‘insertData()’, essentially we are passing the value of ‘data’ to the latter. But what is the value of ‘data’? Its value is the address of the object of type ‘MyDataType’, to which it is referring.

So the crux of the story is that when you are passing arguments of user-defined data types to Java methods, you are not passing the objects as such. In stead of this, you are actually always passing references to those objects. The actual objects keep on lying on the heap. Their references, that are stored in variables of the same type that are on stack get passed to methods. Same is true about return values. Objects are never returned. Instead, their references are returned. Through the references, objects can be manipulated as they keep lying on the heap.

This idea and its understanding can have a profound impact on your programming skills. Once you have developed skeleton code, the next thing you have to deal with is IO in methods. Once this is understood, rest of the stuff becomes easier to comprehend and implement.

Photo by Plutor

If you found an error, highlight it and press Shift + Enter or click here to inform us.

CC BY-NC-ND 4.0 Software Engineering Journey: Implementing Methods by Psyops Prime is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Leave a Reply