r/javahelp • u/juv3nil3 • Aug 18 '22
Codeless Help on Scanners
Why do we need to pass scanner as parameter to a method?? Is it necessary? Tried google but every thing bounced over my head. Can somebody explain this in easy terms. Thanks!
3
u/desrtfx Out of Coffee error - System halted Aug 18 '22 edited Aug 18 '22
If you are talking about a Scanner(System.in) you should only create one instance per program.
This is because the underlying static InputStream in of the System class is common to all such scanner instances.
It is far better to only create one and pass it around than creating multiple. There is absolutely no benefit in multiple such scanners, rather the opposite.
If you close such a scanner instance, the close method also closes the InputStream in which as a consequence means that this InputStream is closed for all instances.
This stops the program from responding to the keyboard at all and there is no way to reopen the InputStream.
1
2
u/fdstkp Aug 18 '22
…you pass a Scanner to a method if that method needs to use the Scanner. we can’t read your mind, if you have a specific example paste it in
0
u/juv3nil3 Aug 18 '22
So either i pass the scanner or I make a new scanner in each of the methods
1
u/juckele Barista Aug 18 '22
To answer your question most abstractly, you pass arguments to methods that you want to access in those methods. You would pass a Scanner object around because you find it a useful abstraction and you don't want to create a bunch of Scanner objects.
Why don't you want to create a bunch of Scanner objects? Maybe you think the code is cleaner and easier to read passing the Scanner around instead of creating new Scanner objects in each method. Performance doesn't really matter at this scale or timeframe (one object instantiation at human reaction speed is essentially zero cost).
Honestly, if you want to use a Scanner all over the place, you can also just make a static Scanner. At scale there's good reasons to pass the Scanner around instead of creating a new one in each method (injecting dependencies allows for better testing and looser coupling of unrelated code pieces), but at the point in your coding career where you use Scanners (Java 101 only really), it just isn't a concern.
1
1
u/Pastoolio91 Aug 18 '22
It’s hard to help without posting your code.
1
u/juv3nil3 Aug 18 '22
It's not my code.. I was trying to solve a question and couldn't..
I looked up the solution and one guy used this approach.. Tried my best to understand the code but again couldn't..
2
u/NautiHooker Software Engineer Aug 18 '22
Without a specific example we cant really give you an answer.
If the method requires the scanner as a parameter then you need to pass it. whether or not the method requires it completely depends on the method,
1
u/Housy5 Nooblet Brewer Aug 18 '22
I'm going to guess it's a Scanner reading on System.in
Basically when reading from stdin you generally only want one instance.
Because when you create a new one in every method it might not get garbage collected because it still has an open stream attached to it. Forming a memory leak.
And if you do close it or it somehow gets garbage collected you wont be able to reopen stdin. So you generally use only one instance for System.in (as global variable or passed as parameter).
At least that's my understanding of it :p
•
u/AutoModerator Aug 18 '22
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.