Passing Parameters

Hi,
I am a newbie and would appreciate it if someone could help me with the following code that I found on an online tutorial.
Thanks in advance,
Gus


class PassObj
{
int n1;
int n2;
// constructor
PassObj()
{
n1 = 0;
n2 = 0;
}

PassObj(int p1, int p2)
{
n1 = p1;
n2 = p2;
}

void multiply(PassObj p1)
{
int temp;
temp = p1.n1 * p1.n2;
System.out.println("Multiplication is " + temp);
}
public static void main(String args[])
{
PassObj obj1 = new PassObj(5,6);
PassObj obj2 = new PassObj();
obj2.multiply(obj1);
}
}

This is a program I got from a Java tutorial online. If I give my understanding of it could someone please tell me if I am correct or tell me where I have gone wrong. I am a newbie so all help is greatly appreciated.

1. The class PassObj has two int variables n1 & n2.
2. These as initialised to zero.
3. In Main, there are 2 instances of the class PassObj created. These are obj1 & obj2.
4. The multiply method is called with parameters 5 & 6 passed to it. The multiplication is done and the result is shown in the console.
My main issues are what is the point of:

PassObj(int p1, int p2)
{
n1 = p1;
n2 = p2;
}
and does the instantiation PassObj obj1 = new PassObj(5,6); actually pass parameters to the above constructor.



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories