0

I have a Chronometer in Class Apple. I want to check what time the Chronometer is at in Class Banana. I do not want to create a Chronometer in Class Banana, nor do I want to make an Apple Object in Class Banana. Is this possible?

Thanks!

Let me know if i can be more specific.

Evorlor
  • 7,263
  • 17
  • 70
  • 141

2 Answers2

1

Is there more than one Apple in your (presumably) game?

If there is exactly one then you can make the chronometer a public static member of Apple and just read it from within your instance of Banana. Like this:

public class Banana {

 public void foo() {
   System.out.print(Apple.chronometer.printValue()); 
 }
}

If there are multiple Apple instances, then you need 1) put them into some sort container; 2) pass in, set, or construct your instance of Banana with a reference to that container; 3) Access the appropriate apple instance in the container, and read the chronometer's value, call its methods, etc. If the chronometer is a private member of Apple, you need to provide an accessor method so it can be accessed by code in your Banana instance.

CBass
  • 983
  • 6
  • 11
  • there is only one Apple. i like where ur heads at! thanks! Apple.chronometer.printValue() is not working tho. any ideas? – Evorlor Apr 20 '13 at 02:28
  • I just made up the printValue method. I don't know what your chronometer is exactly. An instance of a class? A long integer? Whatever "read its value" means, you will have to substitute in the right code. Making it a public static member of Apple, though will work. – CBass Apr 20 '13 at 02:39
  • ah got it. its a bit complicated, but i can figure it out from here. thank you very much!! – Evorlor Apr 20 '13 at 02:40
0

Make a getter for then Chronometer in class apple then from banana just call your getter

Apple:

public Chronometer getMeter() { return mChronometer; }

Banana

Chronometer meter = apple.getMeter();

JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • This would require creating an Apple Object in Class Banana. I was hoping to not need to do that. – Evorlor Apr 20 '13 at 02:14