-2

my question is what is the best way to access Scan and its methods like isEnabled() in the parent.$Child in the below scenario:

I know one way, that is add an abstract method in Parent.$Child like getScan(), implement it in $ScanTest. But is there any other better way to do it.

public abstract class Parent {
    protected abstract void execute() throws SQLException;

    protected static abstract class $Child extends Parent {
        protected void execute() throws SQLException {
            Scan.this.isEnabled(); //Is there a way I can access Scan.this.isEnabled() as Scan is a Parent class  ?
        }
    }
}


public class Scan{
    private static final String TYPE = "scan_version";

    public boolean isEnabled() {
        return true;
    }
    private class $ScanTest extends Parent.$Child {
        public void test() {
            boolean isEnabled =  Scan.this.isEnabled(); 
            //like this, wants to access it in $ScanTest parent class
        }
    }
}
vasu
  • 97
  • 10
  • 1
    `$Child` is extending `Parent`, `Parent` (implicitly) `Object` - `Scan` is not a parent, neither of `$Child` nor of `Parent` - question not clear – user16320675 Jun 23 '23 at 12:38
  • There is no "$Scan" is there any reason you are using `$` for naming? If you take away the funny naming, then $ScanTest can access Scan via `Outer.this`. It isn't a parent it is an outter class, which inner classes retain a reference to. – matt Jun 23 '23 at 14:18
  • Which Scan object do you expect to reach using `Scan.this` in either `Parent` or `Child`? There is no such instance in scope. – Jorn Jun 23 '23 at 14:31

1 Answers1

-2

If you want to access the isEnabled() method within the $ScanTest class, you can do so directly because it is an inner class and has access to the methods of the enclosing class.

public class Scan { private static final String TYPE = "scan_version";

public boolean isEnabled() {
    return true;
}

private class $ScanTest extends Parent.$Child {
    public void test() {
        boolean isEnabled = isEnabled(); // Access the isEnabled() method directly
        System.out.println("isEnabled: " + isEnabled);
    }
}

public static void main(String[] args) {
    Scan scan = new Scan();
    Scan.$ScanTest scanTest = scan.new $ScanTest();
    scanTest.test();
}

}

In the code above, the isEnabled() method is accessed directly within the $ScanTest class using isEnabled(). The output will be isEnabled: true.

Note that I have added a main method to demonstrate the usage. You can create an instance of Scan and then create an instance of $ScanTest using that instance

nischal sharma
  • 479
  • 1
  • 14
  • No, my question is what is the best way to access Scan and its methods like isEnabled() in the parent.$Child. – vasu Jun 23 '23 at 14:13
  • @vasu You cannot. Essentially you are saying, how can I access random aspects of an implementation. There is no reason an implementation of Parent.$Child has an outter class to begin with. If you want the implementation to recognize Scan, then you need to write the methods in the implementation. The more I think about it, it just sounds like you want to accpomplish something, and you have come up with a poor/impossible solution. Step back and ask your real question. – matt Jun 23 '23 at 14:30
  • 1
    I need to achieve this, looks like people here are smart :-) Some time in your existing project you will not have flexibility to do whatever way you want. I need to call Scan.isEnabled(), there are many ways to achieve it. one simple solution is creating new instance of Scan, which I don't want to. another way is mention an abstract method in $Child like: abstract Scan getScan(); and implement it in ScanTest to return Scan.this. – vasu Jun 23 '23 at 16:25
  • I asked this question here to find if there is any other better way. Thanks for all your great answers. – vasu Jun 23 '23 at 16:32
  • This answer looks like ChatGPT – DavidW Jul 13 '23 at 07:34