code works, but have a beginner's question
The following simple code works with netbeans:
package foo4;
public class Foo4 {
public Foo4() {
}
int size;
void setSize(int s){
size = s;
}
public Foo4 go(Foo4 g){
g.setSize(33);
return g;
}
public static void main(String[] args) {
Foo4 f=new Foo4();
f.setSize(66);
Foo4 g=f.go(f);
System.out.printf(f.size+":"+g.size);
}
}
I am trying to understand the contents of this code: A class Foo4 is created, with a zero-argument constructor. Class contains one variable size and a method setSize for setting the size. What I find perplexing is how to explain the functionality of the following lines:
public Foo4 go(Foo4 g){
g.setSize(33);
return g;
}
So, is go a method or a reference ? It is of type Foo4 which should make it a reference right? Also, how is the following line in main methid explained?
Foo4 g=f.go(f); //How do you explain its functionality?
Many thanks to anyone who sheds some light on my questions.
[1087 byte] By [
DeChristoa] at [2007-11-26 15:04:54]

> I am trying to understand the contents of this code:
> A class Foo4 is created, with a zero-argument
> constructor. Class contains one variable size and a
> method setSize for setting the size. What I find
> perplexing is how to explain the functionality of the
> following lines:
>
> public Foo4 go(Foo4 g){
> g.setSize(33);
> return g;
> }
> go a method or a reference ? It is of type Foo4
> which should make it a reference right?
Basically the foo that is called here is a method which is invoked from a aldready existant object f.Now what you are basically doing is that you are creating a reference to the object instance f which is sent as a parameter to the the method,which again changes the value of the size variable to 33 and returns the same refernce back which is recieved.
>Also, how is
> the following line in main methid explained?
> Foo4 g=f.go(f); //How do you explain its
> functionality?
As i just explained above the reference that is returned from the method go is recieved with a object instance which now references the instance returned from the method.
> public Foo4 go(Foo4 g){
> g.setSize(33);
> return g;
> }
> go a method or a reference ? I
Method.
> It is of type Foo4
> which should make it a reference right?
No. It is a method whose return type is reference to Foo4.
What you're doing here is very weird though, and not correct. It's a non-static method, so it's called on an existing Foo4 object. That object manipulates another Foo4 object and returns a reference to it.
What you probably want is something like this: public void go(){
setSize(33);
}
You might have it return this if you want to chain method calls together (like stringBuffer.append(a).append(b).append(c), for instance), but if you don't have a need to do that, then since the method is not creating, fetching, or computing anything, there's no reason for it to return anything.
> Also, how is
> the following line in main methid explained?
>
> Foo4 g=f.go(f); //How do you explain its
> functionality?
I explain it as pointless and confusing, born out of the poorly declared method go. It's a convoluted way to change the internal state of a Foo4.
Thank you for the reply. Although I am still having some trouble grasping the explaination for that.. So, within class Foo4, a method "go" is defined that accepts as argument a reference to the instance of the class Foo4, right? And that reference (g) has also access to the setSize methof of the class....
..Then the reference g is created within main to be "f.go" ?
I mean the class is instanciated and "f" is a reference to the Foo4 object right? But now the Foo4 object contains 2 methods, setSize and go, where go can change the setSize indirectly via g?
It still looks a bit complex to me , but please let me know if my reasoning above is on the right track. Many thanks again.
> Thank you for the reply. Although I am still having
> some trouble grasping the explaination for that.. So,
> within class Foo4, a method "go" is defined that
> accepts as argument a reference to the instance of
> the class Foo4, right?
To an instance of Foo4. There are two references to Foo4--the one that's passed in as an arg and the this reference that's present in all non-static methods. They may refer to the same Foo4 object or to different ones. However, since you're only doing anything with one of the references, there's no need to have two of them. Get rid of the argument and manipulate this.
> And that reference (g) has
> also access to the setSize methof of the class....
Yes. It's a reference to a Foo4, and it's inside the body of Foo4, so it has acess to all of Foo4's members.
> ..Then the reference g is created within main to be
> "f.go" ?
That question doesn't make sense. I don't know what you're asking.
> I mean the class is instanciated and "f" is a
> reference to the Foo4 object right? But now the Foo4
> object contains 2 methods, setSize and go, where go
> can change the setSize indirectly via g?
go can't change set size. Methods don't change other methods. They call them, or invoke them.
> It still looks a bit complex to me , but please let
> me know if my reasoning above is on the right track.
> Many thanks again.
I don't know. You clearly have some terminology confusion. Beyond that, it sounds like you have the basic idea but still maybe don't get exactly how everything fits together.
Yes, thats true, I am learning to code in java and run into questions that i cant answer quite frequently. what i am trying to do is explain this code step by step (even though it looks weird-- since it works, I might deepen my understanding of java if I am able to explain it step by step)
I meant before that in the main method, g is created (or declared?) which is a reference of Foo4. so that makes f and g equivalent in terms that they are refering to instances of Foo4?
So then, g calls go which in turn calls setSize and thats how the size is changed again right?
Hope this isn't getting too frustrating... Thanks for your patience.
> Yes, thats true, I am learning to code in java and
> run into questions that i cant answer quite
> frequently. what i am trying to do is explain this
> code step by step (even though it looks weird-- since
> it works, I might deepen my understanding of java if
> I am able to explain it step by step)
>
> I meant before that in the main method, g is created
> (or declared?) which is a reference of Foo4. so that
> makes f and g equivalent in terms that they are
> refering to instances of Foo4?
Yes thats right.
> So then, g calls go which in turn calls setSize and
> thats how the size is changed again right?
Yes thats right.
public static void main(String[] args) {
Foo4 f=new Foo4(); // 1
f.setSize(66); // 2
Foo4 g=f.go(f); //3
System.out.printf(f.size+":"+g.size); // 4
}
1) A reference variable of type Foo4 named f is declared (not "created"). A new Foo4 object is created. The reference variable f is assigned a reference to that Foo4 object. It "points" to it.
2) We use the f reference variabl to invoke the setSize method on that Foo4 object.
3) We declare another Foo4 reference variable, g. We invoke f's go method, passing f as a parameter. The method returns a reference to a Foo4 (which in this case is the same Foo4 passed as an arg, and the same one on which we're invoking the methods, so there are three references to the same Foo4 object here--this is quite silly, as we only need one). The result of the method (a reference to a Foo4) is assigned to g. Because of the way this method works and how we've called it g and f both point to the same object. You're really complicating things by doing this though, as I explained in m first response.
4) We call f's size method and g's size method. Since both f and g refer to the same object, the same value is returned.
Thank you. This is much more clear to me now.