Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a public thingo at the start

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am getting confused, please define for me ezzackly what line 2 here is? (dont worry about the rest of it, if i mention that stuff i get a 10 post fine i imagine )
1. class X2 {
2. public X2 x;
3. public static void main(String [] args) {
4. X2 x2 = new X2();
5. X2 x3 = new X2();
6. x2.x = x3;
7. x3.x = x2;
8. x2 = new X2();
9. x3 = x2;
10. doComplexStuff();
11. }
12. }
Is line 2 saying that x is a variable of type object X2?? or is it some weird constructor?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 2 declares a variable called x of type X2. No objects were created. This identifier can later be used to refer to an object of type X2.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 2 is no different than something like:
public String s;
public int j;
public Button b;
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies!
so, after line 4 and 5, could a call be sent out like, x.doMethod or something like that, seeing that the variable x was set up as a reference to the class X2?
also, on line 4 and 5, two objects were created, x2 and x3 ... what would the difference be between x2.x and X2.x ,,, - what would it be referencing in each case?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(It's only been six months and one week...)
so, after line 4 and 5, could a call be sent out like, x.doMethod or something like that, seeing that the variable x was set up as a reference to the class X2?
No. You could not directly use x from within the main method, as the main method is static and x is not. Give it a try, and you should see the ol' "cannot reference non-static item from a static context" compiler error message.
also, on line 4 and 5, two objects were created, x2 and x3 ... what would the difference be between x2.x and X2.x ,,, - what would it be referencing in each case?
The compiler will complain if you try to use X2.x, but x2.x would be allowed. x2 is an identifier that refers to an instance of X2. x is defined to be an instance member attribute of objects of type X2. It's an instance member, not a static class member, so it only belongs to instances of X2, it doesn't belong to the class X2.
x2.x would be referencing the variable x that belongs to the instance of X2 that x2 refers to. X2.x would only make sense if x were a static member of X2, which it is not. So, X2.x doesn't make sense because an x only belongs to an instance of X2.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper -
Good to see you back! What's up?
Anyway, I recognize this piece of code, haven't we looked at this before?
Let's look at this diagram (from page 436),

The second line of code:
Island n;
Is pretty close to the code sample you listed. What it means is that every time you create an Island object, it comes with a built in reference variable that you can use (if you want to), to refer to another Island object. Notice how in the diagram we create a little loop of Island objects referring to each other.
I don't know how often you'd do this, but you could (I suppose), make your own little linked lists or something like that.
But really, it boils down to a way for one object of a given type to refer to another object of the same type.
- Bert
[ July 19, 2003: Message edited by: Bert Bates ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic