Should javac find methods outside of an anonymous class of the same name?

This question is a follow up to: Why can’t I call a method outside of an anonymous class of the same name

This previous question answer why, but now I want to know if javac should find run(int bar)? (See previous question to see why run(42) fails)

If it shouldn't, is it due to a spec? Does it produce ambiguous code? My point is, I think this is a bug. While the previous question explained why this code fails to compile, I feel it should compile if javac searched higher in the tree if it fails to find a match at the current level. IE. If this.run() does not match, it should automatically check NotApplicable.this for a run method.

Also note that foo(int bar) is correctly found. If you give any reason why run(int bar) shouldn't be found, it must also explain why foo(int bar) is found.

public class NotApplicable {

    public NotApplicable() {
        new Runnable() {
            public void run() {

                // this works just fine, it automatically used NotApplicable.this when it couldn't find this.foo
                foo(42);

                // this fails to compile, javac find this.run(), and it does not match
                run(42);

                // to force javac to find run(int bar) you must use the following
                //NotApplicable.this.run(42);
            }
        };
    }

    private void run(int bar) {
    }

    public void foo(int bar) {
    }
}


Asked by: Sam769 | Posted: 28-01-2022






Answer 1

This behavior of javac conforms to the spec. See §15.12 Method Invocation Expressions in the Java Language Specification, specifically the paragraph under "Compile Time Step 1" explaining the meaning of an unqualified method invocation:

If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.

In other words, the unqualified method name is searched for in all enclosing scopes, and the innermost "type declaration" (which means either a class or an interface declaration) in which the name is found is the one that will be searched for the whole signature (in "Compile Time Step 2").

Answered by: Tara191 | Posted: 01-03-2022



Answer 2

Sounds like a recipe for ambiguity and fragility to me - as soon as a new method is added in your base class (okay, not so likely for an interface...) the meaning of your code changes completely.

Anonymous classes are pretty ugly already - making this bit of explicit doesn't bother me at all.

Answered by: Kevin245 | Posted: 01-03-2022



Answer 3

Try

NotApplicable.this.run(42);

instead.

Answered by: Anna469 | Posted: 01-03-2022



Similar questions

java - Using methods on anonymous classes

I'm trying to figure out if what I want to do is even possible. I've looked around and google hasn't been super helpful, at least in this specific case. Here's what I'm trying to do. The assignment I'm working on is designed to be run multiple times from the command line, with two possible sets of arguments: ["CREATE" "cardType"] or ["VERIFICATION" "accountNumber" "transactionAmout"] . Every time a "CREATE" flag is...


java - How to test anonymous methods with JUnit or Mockito?

I have simple class but with anonymous block of code. I need to cover this class with tests. public class CleanerTask { private final Logger log = LoggerFactory.getLogger(getClass()); DataWarehouseMessageDao dwMessageDao; int cleanerDelay = 0; TransactionTemplate template; public CleanerTask(DataWarehouseMessageDao dwMessageDao, int cleanerDelay, TransactionTemplate template) { ...


methods - Class Name for Java anonymous class

This question already has answers here:


java - Build an anonymous inner class and call its methods

I searched for this but unfortunately failed to find matches, I have this local anonymous inner class inside a method like this:- new Object(){ public void open(){ // do some stuff } public void dis(){ // do some stuff } }; with 2 methods inside it (open,dis) and I know that if I want to use anyone of them just do ...


Why can I not call methods added to an anonymous class in Java?

If an anonymous class is extending/implementing a class/interface, why can't I add a new method? In other words, this works: class A { void a() { System.out.println("a in A"); } } class B extends A { @Override void a() { System.out.println("a in B"); } void b() { System.out.println("b in B"); } } Why doesn't this work: ...


java - Can I call methods of anonymous classes in any time?

I saw this sample code: public class Main{ public static void main(String[] args){ Pocket<Object> pocket = new Pocket<Object>; System.out.println("test"); pocket.put(new Object(){ String field; void inner(){ ... } }); } } Anonymous-classes doesn't have a class name. So I thought "How d...


java - Is it possible to call several methods from an anonymous class?

I want to know if its possible this in some way pls java 7 or 8 public class App{ public static void main(String[] args) { new Thread(){ public void run(){ //Something } }.start().setName("Something") //Here!! //Something } }


java - Can I access new methods in anonymous inner class with some syntax?

Is there any Java syntax to access new methods defined within anonymous inner classes from outer class? I know there can be various workarounds, but I wonder if a special syntax exist? For example class Outer { ActionListener listener = new ActionListener() { @Override void actionPerformed(ActionEvent e) { // do something } // method is public so ...


java - How to 'pass' methods to anonymous class

I am writing automated tests, and I want each to retry twice. So I wrote a method: public void retry(int retries, Retryable retryable) { for (int i = 0; i < retries; i++) { try { retryable.run(); break; } catch (Exception e) { log.warn(WARN_TEXT, retryable, (i + 1), e); if (i == retries - 1) { ...


java - Why can't I call a method outside of an anonymous class of the same name

The code at the end produces a compile error: NotApplicable.java:7: run() in cannot be applied to (int) run(42); ^ 1 error The question is why? Why does javac think I am calling run(), and does not find run(int bar)? It correctly called foo(int bar). Why do I have to use NotApplicable.this.run(42);? Is it a bug? public class NotApplicabl...


java - Anonymous vs named inner classes? - best practices?

I have a class, let's call it LineGraph, that renders a line graph. I need to subclass it, but the derived class is only used in one place and is coupled to the class that uses it. So I am using an inner class. I see two ways to do this: Anonymous inner class public class Gui { LineGraph graph = new LineGraph() { // extra functionality here. }; }


syntax - Is it possible to make anonymous inner classes in Java static?

In Java, nested classes can be either static or not. If they are static, they do not contain a reference to the pointer of the containing instance (they are also not called inner classes anymore, they are called nested classes). Forgetting to make an nested class static when it does not need that reference can lead to problems with garbage collection or escape analysis.


Can I compile anonymous or inner classes into a single java .class file?

I am writing a Java class that parses log files. The compiled .class file needs to be loaded into a 3rd party monitoring platform (eG) for deployment and invocation. Unfortunately, the 3rd party platform only allows me to upload a single .class file. My current implementation has a function to find the 'latest' file in a folder that conforms to a file mask (*CJL*.log) and uses 2 anonymous classes, one to filter...


java - Anonymous inner class in groovy

I am looking into the groovy-wicket integration and lack of anonymous inner classes seems to be a problem when writing the event handlers. Is there a groovier way of writing this code import org.apache.wicket.PageParameters import org.apache.wicket.markup.html.basic.Label import org.apache.wicket.markup.html.link.Link import org.apache.wicket.markup.html.WebPage /** * Homepage */ class HomePage extends ...


How can I pass an anonymous JavaScript object from Java to JavaScript in GWT?

I'm creating a GWT wrapper round a JavaScript library. One of the JavaScript functions takes an anonymous object as its argument e.g.: obj.buildTabs({ hide: true, placeholder: 'placeholder' }); On the Java side how do I create this type of JavaScript object and pass it to my native implementation? At the moment, on the Java side I have: public void buildTabs(TabCon...


java - Anonymous class binary names

I have the following problem: 1) There is some abstract class A with several anonymous subclasses stored in the static fields of A. There is circular dependency between two of the anonymous subclasses. The code of that abstract class is similar to following: class A implements Serializable { public static final A _1 = new A() { public A foo() { return _2; } ...


Java - Anonymous Inner Class Life Cycle

When using an anonymous inner class as a PropertyChangeListener at what point in the object life cycle is the class garbage collected? After the containing class (SettingsNode) is reclaimed? Should I explicitly remove the PropertyChangeListener in the finalizer of the containing class (SettingsNode)? public class SettingsNode extends AbstractNode { public SettingsNode(Project project, ProjectSettings pr...


java - (nested?) anonymous inner classes for buttons

I've used an anon inner class to get a button obj: Button modButton = new Button("Modify"); modButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { //TODO: link to a pop-up, and do a refresh on exit } }); I want to use this in an arbitrarily sized GWT FlexTable (which is basically an auto re-sizing table). if i do somethi...


java - Access outer variable from inner anonymous Runnable

The following example code (SSCCE) complains that local variable a must be final. public class Foo { final List<A> list = new ArrayList() {{ add(new A()); }}; void foo() { A a; Thread t = new Thread(new Runnable() { public void run() { a = list.get(0); // not good ! } }); t.start(); t.join(0); ...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top