• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

use an instance that is created

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Situation:
3 classes A, B and C

A creates an instance of B. Code in A:


How can I use the created instance of B inside class C? I don't want to create a new B instance.
Is there a way to send the reference of the instance B to C? Is my way good or bad in terms of OO programming?

Thanks

Cwli
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does C have a relationship with A?
 
Sheriff
Posts: 28371
99
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
This is extremely simple:


 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this, but it does not work yet. What am I doing wrong?







 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You said you are creating Instance of class B in Class A, but as I can see you are creating the Instances inside the main method so these objects are local to method and not Instance variables of the Class A.

you even not declare the variable "objectB" so how can you use that.
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which situation (how) can I access the class B in the class C then? I know that class A can access B with the getValue() method, because it has an reference to the instance B.
But with Class C which has no reference I don't know. Creating a new instance of class B in class C is not good because I like to access the only one class B I created.

Perhaps the following code will explain it why I like to do this.

Goal:
- want to create a class ClockPanelDigital, want to seprate analog and digital clock panel

problem:
- my current class ClockPanelAnalog creates the ClockPanel, thus ClockPanelAnalog can call ClockPanel's method addPanel(object ClockPanelAnalog)
This adds the clockPanel to the clock frame. But how do I add another clockpanelDigital to the same ClockFrame?

Extra info:
- I have to let ClockPanelAnalog create the instance of ClockPanel, so that the TimeGenerator instance can add ClockPanelAnalog as an Observer.







 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
....
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:This is extremely simple:




But I don't understand this yet... could someone explain this with a short code example? thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You create new objects by using the new operator.

You can pass these objects to other methods or, also other methods in other classes, or to constructors of other classes. You already have a complete program in which you're passing around objects all the time from method to method.

So surely you must already understand how to pass objects from one place to another, as arguments to a method or constructor. That's what you originally asked, but it's apparently not what your question is really about.

If you want to have one ClockFrame to which you add a ClockPanelAnalog and also a ClockPanelDigital, then you shouldn't put creating the ClockFrame in ClockPanelAnalog. You should probably create the ClockFrame in your main method, in class TimeGenerator, and then pass the ClockFrame object to the constructors of ClockPanelAnalog and ClockPanelDigital, which can then add themselves to the ClockFrame.
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you Jesper,

I was locked in my thinking, didn't know how to create the relationships between the objects so that it would work.
Forgot about the part that I could send an instance referance towards the constructor of another object. Doing so the new instance is able to use
the reference and so access it public methods.

(applying some more anti-rust.... joints are loosing up....)

Cw
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic