Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need an instance of <code>A</code> in class <code>B</code> and invoke <code>f2</code> on that instance. For example, you could instantiate one inside the body of <code>f3</code>:</p> <pre><code>public class B { public void f3() { A a = new A(); a.f2(); } } </code></pre> <p>Another way would be for <code>f3</code> to receive an instance of <code>A</code>:</p> <pre><code> public class B { public void f3(A a) { a.f2(); } } </code></pre> <p>And yet another way, you could have <code>B</code> instantiate one:</p> <pre><code>public class B { private final A a; public B() { this.a = new A(); } public void f3() { this.a.f2(); } } </code></pre> <p>And lastly, <code>B</code> could receive one in it's constructor:</p> <pre><code> public class B { private final A a; public B(A a) { this.a = a; } public void f3() { this.a.f2(); } } </code></pre> <p>The point being that if you want to invoke an instance method on a class <strong>you must have an instance of that class in your hand</strong>.</p> <p>Finally, I notice that you have <code>A.f1</code> invoking <code>B.f3</code> and from there you want to invoke <code>A.f2</code>. So, it looks like your best option here is the <em>second</em> option above. That is:</p> <pre><code>public class A { private final B = new B(); public void f1() { this.b.f3(this); } public void f2() { /* do something */ } } public class B { public void f3(A a) { a.f2(); } } </code></pre> <p>The key here is that we are passing an instance of <code>A</code> to <code>B.f3</code>. The way that we achieve that is by passing the <code>this</code> reference, which is a reference to the currently executing instance. In <code>A.f1</code>, that would be the instance of <code>A</code> that is currently executing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload