question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

val causes intellij to show an invalid error when used alongside an anonymous class

See original GitHub issue

val is supposed to infer the type of a value and make it final, so you should be able to use them in anonymous classes. In fact you can do this! Intellij doesn’t believe you though:

public class ValTest {
    public void doSomething() {
        val foo = new Foo();
        val bar = new Bar();
        foo.setBazListener(new BazListener () {
            @Override
            public Bar onBaz() {
                return bar;
            }
        });
    }
}

When using this code there is an error on bar in return bar that says “Variable bar is accessed from within inner class, needs to be declared final”. I agree, however val indeed does make it final. This code actually compiles and runs fine. I’m currently using android studio 1.2.1.1, and I think it’s based off of idea 14.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mplushnikovcommented, Apr 9, 2016

The update should be available in the next version of plugin (0.12) and will work only for new versions of IntelliJ Idea (2016.2)(build >=146.1154)

0reactions
mplushnikovcommented, Mar 27, 2016

I just filed a issue IDEA-153706 for Intellij.

Read more comments on GitHub >

github_iconTop Results From Across the Web

accessing private member in object class/anonymous object is ...
I was trying to create an object class (to emulate a static class in java) using the latest version of the Kotlin compiler...
Read more >
Java error highlighting showing errors that are incorrect
We have a large Java project using gradle and the error highlighting keeps getting in a bad state on Mac. If I clear...
Read more >
Names for anonymous classes in interfaces are malformed
Names for anonymous classes in interfaces do not start with enclosing class name ( .DefaultImpls is missing). example: import java.util.
Read more >
IntelliJ IDEA 2018.3 (183.4284.131) Release Notes - YouTrack
Feature IDEA‑198897 Parameter hints: Smart argument completion should insert comma if... Feature IDEA‑127245 Open all files from favorites list Feature IDEA‑196706 Multithread model for new...
Read more >
IntelliJ highlights errors that don't exist
try to recreate project jdk, then invalidate caches. Also make sure you didn't configure classpath with different version of the same class.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found