• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing object from one class to another

 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic