• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Accessing objects from different classes

 
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I pass object created in one class to the other class.(Both classes are in same package)
 
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean pass them as method arguments or as constructor arguments?
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question is too vague for us to answer.  Can you expand on your question?  What's the situation where you need this ability?
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find below detailed description,
I have created two classes say Class ABC and Class XYZ.

Both the classes are from same package say Package Sample.

I have created object of Class ABC in that class itself and now I want to use/pass recently created object reference of the Class ABC to Class XYZ.

Please let me know all possible ways.

Also please let me know if you need more detailed description on this.
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Do you mean pass them as method arguments or as constructor arguments?



Yes somewhat like that but not as method arguments or constructor arguments. I should be able to access reference of the object created in multiple classes but initialization will be done once.
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapnil Sanghai wrote:

Campbell Ritchie wrote:Do you mean pass them as method arguments or as constructor arguments?



Yes somewhat like that but not as method arguments or constructor arguments. I should be able to access reference of the object created in multiple classes but initialization will be done once.



Then I don't understand why a constructor argument wouldn't work.






 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Paul Clapham - Apart from this I wanted to know other way to handle this situation.
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramesh Chathuranga wrote:. . . . A is your main class and B is your miner class

Don't call classes minor; the second class contains the real code, so there is nothing minor about it.

. . . . you must make constructer, getter, setter, methods in class B. . . . .

Agree you need a constructor and methods, but I am not convinced that all classes need setter methods.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP: It's really hard to give examples without knowing how you're going to use the object, but here's the shell of a class that you could use to get a database connection object that must be initialized only once:
You would use the class like this: Connection con = DbConnection.getConnection(); The first time you call this it will go through the initialization of the con object.  Subsequent times it would just return the initialized object.
 
Paul Clapham
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapnil Sanghai wrote:@ Paul Clapham - Apart from this I wanted to know other way to handle this situation.



Okay, you could also use a setter method in the User class and pass the Target reference in that way. I will leave you to modify my example to do that.
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Swapnil Sanghai wrote:@ Paul Clapham - Apart from this I wanted to know other way to handle this situation.



Okay, you could also use a setter method in the User class and pass the Target reference in that way. I will leave you to modify my example to do that.



Thanks Paul.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic