After wrapping System.in, if I use System.in, will it always work with its redefined meaning everywhere in each package and in each class throughout the program?
With reference to this lesson:
https://codegym.cc/quests/lectures/questcore.level08.lecture07
Scope of the wrapping System.in
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
29 June 2021, 20:31solution
Changing system.in only changes how the JVM processes calls to it from that instance of the program. Say you had code in a class like this:
which changes the System.in to a supposed filename (you can change that to any file on your computer). Then make a separate "Test" class with code like this
Now run the first class, which changes the System.in and creates an infinite loop. Then run the code from the second class. The second class will still be expecting user input and not output the first line of the file. The running of the second class doesn't stop the running of the first class, but they are running on different instances.
Now change the code in the first class to this:
which will start the test class code from the same JVM instance of the original code. This time it will output the first line of the file. +2
Gellert Varga
29 June 2021, 21:46
Very good explanation. Thank you!
0