Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.