It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Parameter Passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parameter Passing" Watch "Parameter Passing" New topic
Author

Parameter Passing

Gus Hayes
Greenhorn

Joined: Jan 06, 2013
Posts: 13
Hi
I am a newbie to Java and found this tutorial on the web and would appreciate if someone could help me with it.
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.
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
I am not sure what you want exactly. But, i will try to help. PassObj(int p1, int p2) is to allow you to initialize the variables of a PassObj instance.
The instantiation PassObj obj1 = new PassObj(5,6); actually pass parameters to the above constructor - yes, but here parameters are passed by copy.
How to check when things are passed by copy or value? Try the code below.



And read this link too.
http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html


Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14
.
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14
Gus Hayes wrote :
The multiply method is called with parameters 5 & 6 passed to it.


*First of all the multiply method is called with the PassObj object passed to it.

The point of :

is to assign the values p1 and p2 to instance variables n1 and n2.

When you are instantiating PassObj "PassObj obj1 = new PassObj(5,6);" the PassObj(int p1, int p2) constructor will be called and values will be assigned.

The constructor call is shown in bold in above line.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Where did you find that tutorial? We require you quote it, so as not to breach copyright.
Gus Hayes
Greenhorn

Joined: Jan 06, 2013
Posts: 13
Thanks for the replies Guys ... The source of the tutorial is http://www.java2all.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
Thank you for the tutorial name: was it this example?

Lots of people get confused about pass by value and pass by reference, particularly when people write things like “Primitives are passed by value and Reference types are passed by reference” (see for example this link (No 6)). You can see examples of real‑life pass‑by‑value; cats do it naturally (‍), as you can read in this thread (if you have a week to spare).

Briefly: if you pass anything to a method, you cannot change the original value. If you could, that would be pass‑by‑reference. You can however change the state of a mutable object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parameter Passing
 
Similar Threads
and yet another recursive problem
Generics Compiler error
passing parameters to a method.
Fibonacci
fyi: pre/post increment with exception thrown on the line