wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes returning information from an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning information from an object" Watch "returning information from an object" New topic
Author

returning information from an object

Peter Kay
Greenhorn

Joined: Nov 24, 2003
Posts: 4
Im still stuck with this so i will try explaining the problem again. I think this time it might be with the way i am storing variables inside objects which might be causing the problems.
I have a class called ObjectStore which has 2 strings and an int in it.
ObjectStore(String one, String two, int three) {this.one = one; ...
I then have another class called ObjectStoreTwo which stores an object in it
I then have a third class which uses some methods to collect the string + ints and store them in objectStore and then it stores ObjectStore in the object field of ObjectStoreTwo.
Now i am trying to call the data back using another class. At the moment i can use ObjectStore.one to get the last set of data to be used there but ObjectStoreTwo.ObjectStore.one says cannot resolve symbol.
I have a method Look(number) which gets the ObjectStoreTwo with the number assigned to it, but it doesnt work if i try to use either:
System.out.println (Look(1).ObjectStore.one);
or
Object ThisObject = Look(1);
System.out.println (ThisObject.ObjectStore.one);
Any ideas?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Peter,
First, let me answer your question straightaway: you're referring to the members of an ObjectStore object correctly as one, two, and three; these are the member variable names. But I don't understand why you're not using the member variable name of the ObjectStore variable inside ObjectStoreTwo to refer to the ObjectStore object -- i.e.,

Hopefully that makes sense.
Now that I've got that out of the way:
First, I hope you're just making these names up for the sake of example. Having a number of classes with such similar and yet meaningless names, and variables with meaningless names like "one" and "two," makes code virtually impossible to understand, as should be clear from our discussion so far. I don't know what ObjectStore is, maybe it's a can of house paint:

and maybe ObjectStoreTwo is a shelf:

Talking about these classes would be much easier, yes?
Now, second, in general, member variables should be private, and the code that manipulates them should be in the class where they live. The question of how to access a member of a member of a member should never come up -- classes should work with their own data, and if some other class wants information from an object, it should call a method to get the information.


[Jess in Action][AskingGoodQuestions]
Peter Kay
Greenhorn

Joined: Nov 24, 2003
Posts: 4
Well to be honest its part of a project. I have a linked list (bunch of objects, one set as the start with each one pointing to the next one).
The code for the linked list as well as the objects is already done and probably shouldn't be changed.
The objects in this are the OBjectStoreTwo in that they contain another object from a different class. Im trying to store the PaintCan inside the objects and use a different class (Shelf) to add/remove and look on the shelf.
In the shelf class i have a method:
public void AddPaintCan(String brand, String color, int size)
{ paintCan = new PaintCan(brand, color, size); }
The linked list is declared as LL
With another method which uses this method:
[input for brand, color and size. All working and tested as well as the AddToLinkedList method]
LL.AddToLinkedList(Beginning, paintCan);
I can then use a System.out.print(LL.Look(1)); to show the pointer to the paintcan or:
System.out.print(paintCan.brand); to show the brand from the last entered paintcan.
The problem is when i try to use something like:
Object thisPaintcan = LL.Look(1);
System.out.print(thisPaintcan.brand);
I am sorry if this is confusing (its confusing for me as well ).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30


Ah, OK, now I see what the issue is. The linked list class deals with Objects, and when you get an Object out you want to go back to referring to it as a PaintCan (or ObjectStore in this case.) You can use what's called a cast. A cast tells the compiler that, even though a method returns, for example, Object, you know for a fact that it's actually a PaintCan. You write
PaintCan thisPaintCan = (PaintCan) LL.Look(1);
The name of the real type in parentheses like that is the cast. Now thisPaintCan is a variable of type PaintCan, and you'll have no trouble calling its methods or accessing its variables.
Peter Kay
Greenhorn

Joined: Nov 24, 2003
Posts: 4
Thanks a lot it all works now
I have never tried using casting with my own objects, ive only ever used it with numbers so it never crossed my mind to even try it.
Thanks again, really helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning information from an object
 
Similar Threads
Boxing/conversion question. Thanks.
Problem with Thread..
Solution to DuplicateKeyException
toString()
try catch