Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Passing Parameters Posted by loosehead on 30 Jan 2013 at 2:51 PM
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.



Thread Tree
loosehead Passing Parameters on 30 Jan 2013 at 2:51 PM
onepphire Re: Passing Parameters on 7 Jun 2013 at 10:20 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.