-1

Why do we have to create a new object "myObj" referring to the input stream but not just using Scanner.nextLine() directly? Example from tutorial

If the nextLine() method is from the imported package, can't we just call it as predefined method whenever (just like "printf" function in C)?

I understand that both works but just want to learn the logic behind creating a new object instead. Thanks

unacorn
  • 827
  • 10
  • 27
  • To begin with, `Java` is `Object-Oriented` and `C` is not. So you can't create objects in `C` anyway. And getting back to calling methods, if you look at `Scanner Class` in `Java Docs`, you will find that the method `nextLine()` is not defined as `static`. So you will have to create an object to call it. Only `static` methods can be called using class name directly. – Mathews Mathai Nov 24 '19 at 04:59
  • Where would `Scanner.nextLine()` get the line from? The console? A file? Which file? `Scanner` can be used to read sources other than user input from a console. --- **Read the documentation**, i.e. the javadoc of [`Scanner`](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#constructor.summary), to see the list of ways to create a `Scanner` object, i.e. how many different sources it supports *(which is 6: `File`, `InputStream`, `Path`, `Readable`, `ReadableByteChannel`, and `String`, as of Java 8, and `InputStream` can itself be many sources, e.g. `System.in`)*. – Andreas Nov 24 '19 at 05:43
  • Got it thanks @MathewsMathai! Yes I am aware that C can't create objects, was just using that as an analogy to understand why Java works slightly different from C in this matter. I did read the documentation and do understand where does the Scanner.nextLine() gotten support from the sources as well. Thanks for your clarification! – unacorn Nov 24 '19 at 05:56
  • Because It Isn't Static. – user207421 Nov 24 '19 at 08:56
  • @user207421 thanks for your feedback. I don't think this is a duplicate of the question that you linked. The error caused by static vs non-static is clear to me. I am asking this question in the context of understanding this more in detail in reference to importing package and logic of these altogether though..This will be helpful to new learner of OOP and drilling down to think how it works. – unacorn Nov 24 '19 at 10:39

2 Answers2

2

This is a question relating to the understanding of Classes and Objects.

Think of the class Scanner as a blueprint. It can read data input from not only the Keyboard, but also Files, and other Sources that have InputStreams, like a network connection. Due to its varied uses, Scanner has to be told which source to read from.

This is why, you create an object of Scanner, by using

Scanner obj = new Scanner(System.in);

System.in tells the scanner to read the data from the keyboard. You could provide a file or another InputStream and the Scanner read from there.

This is why there is no static method in the Scanner class to read from the terminal, without creating an object.

Ananay Gupta
  • 355
  • 3
  • 14
0

First System.in is InputStream it is binary data But You need input that you understand which String or int etc that require need change bytes to Int Scanner Class method do that for you. Scanner just don't do conversion of data also manages input instances.for eg when stop taking input like that

here check out the code

 public static void main(String[] args) throws IOException {
    // write your code here
                Scanner myObj = new Scanner(System.in);

                System.out.println("Enter name, age and salary:");
                byte[] arr= System.in.readAllBytes();
                System.out.println("here your binary output ");
        for (byte a:arr) {
            System.out.print(Integer.toBinaryString(a));
        }
    }

enter image description here

**Make Sure to use Ctrl+D after enter the input**
Morpheus
  • 550
  • 1
  • 9
  • 23