Skip to content

Instantly share code, notes, and snippets.

@bufferings
Last active March 21, 2018 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bufferings/a07735229b57444374b83b3789be76be to your computer and use it in GitHub Desktop.
Save bufferings/a07735229b57444374b83b3789be76be to your computer and use it in GitHub Desktop.
JShell で Effectively Final 遊び
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
/**
* こんなつぶやきを見かけたので、debugオプションつけて雰囲気見てみたお話。
* https://twitter.com/mike_neck/status/976358507011178496
*
* jshell> var list = List.of("foo", "bar")
* list ==> [foo, bar]
*
* jshell> Stream.of("baz").forEach(s -> list = Collections.singletonList(s))
*
* jshell> list
* list ==> [baz]
*/
public class Main {
public static void main(String[] args) {
System.out.println($JShell$12.do_it$());
System.out.println($JShell$17.do_it$());
System.out.println($JShell$21.do_it$());
}
}
class $JShell$12 {
public static List<String> list;
public static Object do_it$() {
List<String> list_ =
List.of("foo", "bar");
return list = list_;
}
}
class $JShell$17 {
public static Object do_it$() {
Stream.of("baz").forEach(s -> $JShell$12.list = Collections.singletonList(s));
return null;
}
}
class $JShell$21 {
public static Object do_it$() {
return $JShell$12.list;
}
}
@bufferings
Copy link
Author

bufferings commented Mar 21, 2018

[foo, bar]
null
[baz]

Process finished with exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment