• Post Reply Bookmark Topic Watch Topic
  • New Topic

strings inside object inside object  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having difficulties getting strings to output when they are in an object which is inside another object. Best i have done so far is display the pointer for the 2nd object which is not very useful.
Ill ignore the first object for the moment because i am able to access the 2nd object. Ill call the 2nd object "secondObject" and one of the strings "stringIWant"
If i use System.out.println(secondObject) then it displays the pointer so i tried using System.out.println(secondObject.stringIWant) but this gives the errors:
cannot resolve symbol
symbol : variable stringIWant
location : class.java.lang.Object

There any ideas on how i can get the string to output?
Thanks,
Peter Kay.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
I gather from what you've said that you have, for example, a class called FirstObject and a class called SecondObject. The FirstObject class has an instance variable of type SecondObject called secondObject. The SecondObject class has an instance variable of type String called stringIWant.
For example:

If this is the case (or similar) you should be able to access the String, stringIWant, through an instance of FirstObject like this:
FirstObject firstObject = new FirstObject();
String theString = firstObject.secondObject.stringIWant;
System.out.println(theString);
You may also want to take a look at the access for your variables as well (whether they are public, private, or default - default being neither public nor private e.g.
String stringIWant;
vs
public String stringIWant;
Hope this helps.
--BW
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!