This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Pass by value, reference, the this reference and invoking constructor?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pass by value, reference, the this reference and invoking constructor??" Watch "Pass by value, reference, the this reference and invoking constructor??" New topic
Author

Pass by value, reference, the this reference and invoking constructor??

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Q.1 Pass by value
"Java passes arguments only by value, the argument cannot be changed by the method called."
Does it mean the value of the variable is copied as the argument of the method, so the original value does not change?
Q.2 Pass by reference
"When an object instance is passed as an argument to a method, the value of the argument is a reference to the object. The contents of the object can be changed in the called method, but the object reference is never changed."
e.g.
public class MyDate
{
int day;
int mth;
int year;
public void changeDate(MyDate ref) //A method to accept pass by object
{
ref.day = 20;
ref.mth = 10;
ref.year = 1970;
}
public static void main (String [] args)
{
MyDate MyBirth = new MyDate();
MyBirth.day = 10;
MyBirth.mth = 7;
MyBirth.year = 1960;
//An object is created now.
System.out.println("Day = " + MyBirth.day + " Month = " + MyBirth.mth + " Year = " + MyBirth.year);
System.out.println("\n");
MyBirth.changeDate(MyBirth);
System.out.println("Day = " + MyBirth.day + " Month = " + MyBirth.mth + " Year = " + MyBirth.year + ".");
System.out.println("\n");
}
}
"the value of the argument is a reference to the object" --> Does it mean that the memory address of the object?
"The contents of the object can be changed in the called method" --> Just like the above example, the attributes of the MyBirth object changed when it was passed to the changeDate method. But, the object reference (memory address is never changed). Is it?
Q. 3 The this Reference
e.g.
public class MyDate {
int day, mth, year;
public void born() {
Birthday bDay = new Birthday(this); //Why there are another class to new a "this" object? What does it mean? Any graphical explanation?
sendAnnouncements();
}
}
"When you call a method in a separate class and pass a reference to the current object as an argument."
Does it mean the bDay object contains the attributes of day, mth and year but it is under the Birthday class? I don't understand "pass a reference" mean? pass the MyDate attributes or pass the Birthday attrubutes as reference?
Q.4 Invoking overloaded constructors
e.g.
public class Employee {
private String name;
private int salary;

public Employee(String na, int sa)
{
name = na;
salary = sa;
}

public Employee(String n)
{
this(n, 0); //What does it mean? like strcpy(n, 0) in C++? i.e. copy the '0' to the argument n. n is refered to the String name attribute by this???
}

public Employee
{
this("unknown"); //What does it mean?? pass unknown to which attributes?? to both name and salary??
}
}
"Any call to "this", must be the first statement in any constructor." //Does it like super must be placed in the 1st statement?
Thanks in advance
Andrew
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Q1. Yes
Q2.

Just like the above example, the attributes of the MyBirth object changed when it was passed to the changeDate method. But, the object reference (memory address is never changed). Is it?

Correct
Q3.

Birthday bDay = new Birthday(this);
//Why there are another class to new a "this" object? What does
it mean? Any graphical explanation?

You are not "new"ing "this". You are passing "this" as an argument into the ctor of the Birthday class.
Q4. You have three ctors for this class.
The statement "this(n, 0);" in one ctor just calls the ctor with two arguments, passing a default value of 0 for salary.
The statement "this("unknown");" calls the ctor with one argument, passing in a default value of "unknown" for name - this in turn calls the ctor with two arguments ...
I think you have a little typo. The default ctor needs to look like this: "public Employee()".

Please ignore post, I have no idea what I am talking about.
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Yes, sorry for my typo and thanks a lot for your explanation.
Andrew
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
As far as questions 1 and 2 go, try not to think of it in terms of pass by value or pass by reference. It is quite different than C++. What I did basically is forget all that when dealing with Java, and worry about what it really is doing, not what to call it. As far as what it really is doing, you are correct in your previous statements. Java is built slightly on the C++ syntax, but it is very different. As long as you know how it works, don't worry about whether it's by value or by reference (this has been a large and heated discussion between many Java programmers for a while now, you'll get a different answer every time you ask the question).
Same thing with the question of pointers. In Java as you know, they're called references. Whether or not they hold a pointer to the actual object is irrelevant, we know how it works, we know how to use it, that's all that matters. It's a reference, not a pointer, very different. If you want to think in C++ terms, then think of the interface and the implementation of a method or class. If you want to know how to use the method or class, you look at the interface, you need not look at the implementation because you shouldn't be concerned about how the method is doing what it is doing, just know that it is doing it correctly and that you are using it correctly. And I only use C++ as a point of view because that's where a lot of people migrate to Java from (myself included).
I've rambled enough, you get my point.

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for your sound advice.
Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass by value, reference, the this reference and invoking constructor??
 
Similar Threads
What is "this" keyword?
Inheritence query??
Polymorphism, arguments and instanceof questions
Garbage collection-Marcus exam
What is object?