Community
Participate
Working Groups
Ctrl+2, l allows to assign variables even without importing there type first but this does not work for Optional. package snippet; public class Snippet { public static void main(String[] args) { // Using a String String s = "Hello"; Optional.ofNullable(s) -> PRESS CTRL+2, l, does not work } } package snippet; public class Snippet { public static void main(String[] args) { // Using a String String s = "Hello"; new Todo()-> PRESS CTRL+2, l, does work } }
Consider the following example: package snippet; public class Snippet { public static void main(String[] args) { // Using a String String s = "Hello"; // Optional.ofNullable(s) // [1] // Todo.instance(null) // [2] // new Todo() // [3] } } Without import, Ctrl+2, L works only at [3] and not at [1], [2]. It works at [3] as the type of variable declaration is known with the constructor call "new Todo()", and the type need not be resolved. At [1] and [2] it doesn't work as the type of variable declaration to be created is not known until the Type is resolved (import is added) and the method's return type is known.
Created attachment 257543 [details] Animated Gif See the animated GIF for a demonstration. Example snippet: // for pasting // Optional.ofNullable(s) // new Todo() public class Main { public static void main(String[] args) { // Using a String String s = "Hello"; // After I paste the text I try Ctrl+2, l Todo todo = new Todo(); } private class Todo { } }
See comment 2
(In reply to Lars Vogel from comment #0) > Ctrl+2, l allows to assign variables even without importing there Please reopen if you can provide a link to documentation that says so.
(In reply to Dani Megert from comment #4) > (In reply to Lars Vogel from comment #0) > > Ctrl+2, l allows to assign variables even without importing there > > Please reopen if you can provide a link to documentation that says so. Works for new Todo() even if you move it out.
(In reply to Lars Vogel from comment #5) > (In reply to Dani Megert from comment #4) > > (In reply to Lars Vogel from comment #0) > > > Ctrl+2, l allows to assign variables even without importing there > > > > Please reopen if you can provide a link to documentation that says so. > > Works for new Todo() even if you move it out. It does not resolve the type but because it is a constructor we know what name will be on the LHS. In your examples (method call or field) the type cannot be known until the import is done. Normally I would not want a certain quick assist/fix to do more than advertised, but in this case where it doesn't do anything, we could do better. I mark this as duplicate of bug 444870, since once the import is done as first step, the type of the method or field will be available. *** This bug has been marked as a duplicate of bug 444870 ***