This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing object from one class to another

 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on XML file parsing and I am using SAX parser. I have created XMLParser class. In that I have,

and

In CustomDefaultHandler, after parsing, I have stored all data in List of custom objects, now I want this list in my other class to do some business processing and ultimately saving into DB. How can I pass it? The default parse method returns void (understandably). One way I can think of is creating static list object List<CustomObject> customObject in XMLParser class and setting its value in CustomDefaultHandler class.

Thank you.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your CustomDefaultHandler has an instance variable containing a List<Whatsit>, then? If that's what you were saying, then add a method to CustomDefaultHandler which returns that List. Like this:

Then after the parsing is finished (i.e. after the "sp.parse" line of code) you just call that method and you've got your list. Of course you'll have to keep a reference to the CustomDefaultHandler object, rather than just throwing it away like you do, so you can call that method after parsing.
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can it be so simple?

Yeah, it does make sense. You have an instance variable that you want to make available to outside world. Use getter method. Simple! I don't know why it did not occur to me. Thank you Paul.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess: You were thinking of CustomDataHandler as a class which was doing things, directing the progress of the program. And therefore you wanted to have it direct the end result to your XMLParser class. (Hence your speculations about copying the result to a static variable there.)

Whereas if you think of it as a class whose responsibility is to just do things when it's given little bits of data (which is what a SAX DataHandler really is) then it's obvious that it should also be asked to return the results of what it did.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic