aspose file tools*
The moose likes Beginning Java and the fly likes Object of an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object of an object" Watch "Object of an object" New topic
Author

Object of an object

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

These days I had in my head the concept of object in an object. Maybe it looks something like that (it does not work, it is an infinite loop while running):

class A {

int i = 6;

A x = new A ();

public static void main (String [] args) {


A y = new A ();

}
}

Finally I wanted to have something like that:

System.out.println(x.y.i);

Does anybody understand my thoughts?

Regards
Urs


SCJP 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

What exactly is your question about this? Yes, you can have a member variable that is of a non-primitive type. Ofcourse if you do it the way in the code you demonstrated above you will get an infinite recursive loop. Do you understand why?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Sounds more like a beginner's question to me . . .

Have you got a book which explains OO design? Have you read the Java Tutorial about classes?

What you have is a class which creates two objects of itself, one static (inside the main method), the other instance type (which you call x). You cannot unfortunately print x and y from the same place because the static variable "x" doesn't have access to the instance variable "y", and from where the instance variable "y" is the static variable "x" is out of scope.

Presumably you get infinite repetition (not actually an infinite loop) because every time you create an "x" it tells itself to create another "x"!
It is difficult to see what you are trying to do. Read basic object orientation and get that concept clear first.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

Indead I do not properly know, what I am looking for. I wanted to "encapsulate" an object inside an other object.

Is it possible, that an object "lives" inside an other object?

A class is a collection of methods & varialbes. But as you can see, class A contains already an object, it is x. For me it looks like class A contains a method, a variable and an object. Now I instantiate class A inside the main method. Thus I have the feeling, the object y contains object x?

I made the experience, that this might not be the truth.

Regards
Urs
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Let's say "references" instead of "contains". An object variable is a reference to another object. Here the variable "a" is a reference to a String object.

An object can sure enough have references to other objects of the same type. But when you initialize the reference as you did above you will get the infinite recursion Jesper mentioned. Follow through what happens on this line:

It makes a new A, which ...

So you can build this little set of objects, but you'll have to lose that new A() and set the references in some other way. Maybe


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Look at this simple class.

Here you have a class that has a member variable called 'name' which is a String. A String is an object. So voila, here you have a class (Example) that "contains" an object.

The recursive loop in your application happens because you have an object of type A inside class A (itself). So if you create a new A object, then the member variable x will be initialised by creating a new A object, and that new A object contains a variable x that will be initialised by creating a new A object, and that new A object contains a variable x that will be initialised by creating a new A object, ... until you run out of memory.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi Stan

Your answer is very good. But could you give the whole source code? I tried with the fragment, of course it did not work.

Best Regards
Urs
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Urs Waefler:
could you give the whole source code?


Just change your original class definition from

A x = new A();

to just

A x;


[Jess in Action][AskingGoodQuestions]
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

Now I have changed it. This is my source code:


class A {

int i = 6;

A x;

public static void main (String [] args) {

A y = new A();
y.x = new A();
System.out.println(y.x.i);

}
}


It works fine. Still I do not properly understand the line:

y.x = new A();

What do you think about that line - what happens?

Regards
Urs
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

"It works fine. Still I do not properly understand the line:

y.x = new A();

What do you think about that line - what happens?"


In detail, this is what this means:

1. You have a variable named 'y' of type 'A'. In class A, you have a member variable named 'x'.
2. Your variable 'y' points to an object of type 'A' that you've created in the line 'A y = new A();'
3. The expression 'y.x' refers to the instance of the member variable 'x' in the object that 'y' points to.
4. In the line of code above, you are creating a new object of type 'A' and you make 'y.x' refer to that object.

Have a look at Lesson: Classes and Objects in The Java Tutorial; especially have a look at how declaring and using member variables works.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi Pete Stein

JavaRanch is not the right place to ask questions? Remember that this is Java in General (beginner). Please do not judge, it helps nobody. Your comments are only usefull, if it is a proper answer. Other things are not constructive. Again I ask you to be constructive.

I know, there are some weaknesses. That is one reason, that I join JavaRanch. OK?

Althought your comment showed me my current status. I do not take it personal. I am looking forward to learn more.

Regards
Urs
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Dear Pete Stein

Thank you. Could you help me again? I am looking for a deeper encapsulation. Is it possible? This is a pseudo source code (it does not work):


class A {

int i = 6;

A x;

public static void main (String [] args) {

A y = new A();
A y.x = new A();
z.y.x = new A();
System.out.println(z.y.x.i);

}
}


Do you understand? We have to start at that point:

System.out.println(z.y.x.i);

Which kind of source code must be written, that the line above works? These kind of questions - challenges - are very good. They lead you & me to a deeper understanding.

What is the solution?

Best Regards
Urs
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In general you're building a chain of object references. You're saying z is an object with a variable y of a type that has a variable x of a type that has a variable i. For this particular example, what does the compiler tell you?

is not a valid statement. You have an object y and you want to set its variable x to a new A(). You don't need to declare what type you're working with because the compiler already knows the type of y.x. Try leaving out the "A" at the beginning.

Then these lines

say "z cannot be resolved". You haven't told the compiler what z is yet. It seems to be something with a variable called "y". Is that what you meant? Tell us as clearly as you can what you want "z" to be, and I bet you'll answer your own questions about how to fix it.

BTW: Objects that contain references to other objects of the same type are quite common in trees and linked lists. This is a good exercise to get a grip on how that works.
[ March 19, 2007: Message edited by: Stan James ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Urs: Listen to Stan James, he speaks the truth. Keep asking questions no matter how usual or unusual. I'm going to shut up, cuz I think I'm in over my head in this thread. The quote that best applies to me is probably "a little knowledge is dangerous". Again, sorry for misspeaking.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi Stan

You gave me a good idea. I want to have a look again at:

System.out.println (z.y.x.i);

It is a chain. I try to make a chain. I feel, there are some limits. How dose a chain work?

This is the initial source code:


class A {

int i = 6;

A x;

public static void main (String [] args) {

A y = new A();
y.x = new A();

System.out.println(y.x.i);

}
}


It works fine, I can understand. But I do not have an idea, what do I have to add, thus the statment System.out.println (z.y.x.i); works?

Regards
Urs
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You're telling the compiler to look at variable "z" and you don't have such a variable. So you'll need to add that.

What type should "z" be? You're telling the compiler it is some type with a variable "y". So you'll have to write a class with a variable "y".

What type should "y" be? You're telling the compiler it is some type with a variable "x". It could be the class A you are already playing with, but we won't know until we see z.

Try adding a variable "z" inside your method. You may have to write a new class with a variable "y", too. See what you can make of it yourself. It's much more fun that way!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object of an object