-4

I'm new to Java. I'm trying to solve the question below, which requires me to answer the [???] part. I have already read inner class, and reflection on multiple sources but still cannot solve it.

class A{
  static class B {}

  static class C {
    B b;
    C(B b) { this.b=b; }
    int foo() { return 42; }
  }
}

public class Exercise{
  public static void main(String [] arg){
    assert ([???].foo()==42);
  }
}

I tried

    assert (A.C.foo()==42);

and got error: non-static method foo() cannot be referenced from a static context

I cannot change any other part of the code, but the [???] part. This is how the challenge is designed. This challenge is classified as easy so maybe I'm missing something critical ?

Osca
  • 1,293
  • 2
  • 14
  • 27
  • 2
    "I tried everything" for example? Please add your attempts to the question. – Federico klez Culloca May 30 '22 at 09:33
  • I would suggest reading up about inner classes, and about constructors. This is quite a tricky problem, but I think you'll gain more from continuing to plug away at it, than you'd gain from someone answering it for you. – Dawood ibn Kareem May 30 '22 at 09:34
  • 5
    `foo()` is an instance method of the class `A.C`, so you require an instance of that class. Did you try creating one? – khelwood May 30 '22 at 09:34
  • Have you tried `new C(new B()).foo()` too? – tevemadar May 30 '22 at 09:36
  • Your code doesn't have inner classes, it has nested (static) classes. – Mark Rotteveel May 30 '22 at 09:37
  • 1
    @tevemadar if they did it wouldn't compile. – Federico klez Culloca May 30 '22 at 09:38
  • 2
    @FedericoklezCulloca ok, I haven't noticed the separate outer class and thought about https://ideone.com/JrLheF only. But then `new A.C(new A.B()).foo()`, as they're `static`. – tevemadar May 30 '22 at 09:41
  • Offtopic: Java is a high level language. Your code will be compiled and not interpreted. [Read more](https://stackoverflow.com/questions/17253545/scripting-language-vs-programming-language) – akop May 30 '22 at 09:50
  • @tevemadar it returns error `error: cannot find symbol assert (new C(new B()).foo()==42);` – Osca May 30 '22 at 10:16
  • @tevemadar you are right, it's `new A.C(new A.B()).foo()`. thank you – Osca May 30 '22 at 10:19
  • @tevemadar could you please explain the answer a bit ? thank you – Osca May 30 '22 at 10:22
  • 1
    @Osca, as the message says, `foo()` is not a `static` method, it has to be called on an object, and that's what your question is about. Then a convenient thing is that classes `B` and `C` are `static` classes, so they don't need an `A` object, they can just be referred directly as `A.B` and `A.C`. And so it happened, as `A.C` had to be instantiated, but it needs a `B` (which is an `A.B` for the outer world, like us) in its constructor. – tevemadar May 30 '22 at 10:29
  • 1
    If they weren't `static`, that case would need `A` object(s) to instantiate the inner classes, for example `new A().new C(new A().new B()).foo()` as a one-liner. A single `A` object from a variable could be used too of course. – tevemadar May 30 '22 at 10:35
  • You don't need an instance of `B`: you can just use `new A.C(null).foo()` – khelwood May 30 '22 at 10:49
  • @tevemadar I got it now, thank you very much! – Osca May 30 '22 at 23:39

1 Answers1

-2

Here's a couple options that might work:


  1. Make an instance of C class. It actually requires a B instance to make one, so you might consider something like this:
public class Exercise {

  public static void main(String[] arg) {
    C c = new C(new B());
    assert (c.foo() == 42);
  }
}

If you really want to make changes only in that little spot - consider this option:

public class Exercise {

  public static void main(String[] arg) {
    assert (new C(new B()).foo() == 42);
  }
}

  1. Make a foo method static.
static int foo() {
      return 42;
}

Then you'll end up with this:

public static void main(String[] arg) {
    assert (C.foo() == 42);
}
RickHeadle
  • 30
  • 6