File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes a public thingo at the start Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a public thingo at the start" Watch "a public thingo at the start" New topic
Author

a public thingo at the start

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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?


giddee up
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
line 2 is no different than something like:
public String s;
public int j;
public Button b;


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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

Joined: Dec 10, 2001
Posts: 7023
(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

Joined: Oct 14, 2002
Posts: 8883
    
    5
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 ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a public thingo at the start