0

A question from core java. I am trying to use scanner class by passing java.io.InputStream variable just like System.in. I dissemble java.io.System class and I found that a field is declared as "public static final java.io.InputStream in;" so I also declared a filed of same type as "myin". but it is giving compile time error stating that - error:variable myin might not have been initialized. I just wanted to know how can I declared "in" and "out" variable of my own. Please help me out. any help will be highly appreciated.

import java.util.Scanner;
import java.io.*;
class Test
{
  public static final java.io.InputStream myin;
 public static void main(String[] args)
 {
    int i;  
    Scanner sc = new Scanner(Test.myin);
    System.out.println("Enter a number:");
    i = sc.nextInt();
    System.out.println(i);  
 } 
}
Pushkar
  • 11
  • 1
  • 1
    Initialise `myin = System.in` – Aditya Arora Jan 26 '22 at 04:28
  • Thanks aditya.. in is the same type of variable as I have created myin. In System class there is a in variable of same type what I am creating.. then I am not able to understand the need of such type of initialization. – Pushkar Jan 26 '22 at 06:05
  • There isn't a need to initialise your own variable for taking input. – Aditya Arora Jan 26 '22 at 06:07
  • Check this - [What is System.in](https://stackoverflow.com/questions/24786399/what-is-system-in/24786425#:~:text=System.in%20is%20an%20InputStream,line%20arguments%2C%20or%20configuration%20files.&text=This%20is%20a%20separate%20input%20mechanism%20from%20Java%20IO.) – Aditya Arora Jan 26 '22 at 06:10

1 Answers1

1

The final modifier means that the variable has to be initialized exactly once, but you don't initialize it at all. However in your case, the variable is not needed, you can initialize the Scanner with Scanner sc = new Scanner(System.in);.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • Thank you sir.. I am trying to create something like System.in... To pass in scanner class.. why can't I pass myin variable which is declared same as in declared in System class.. In above code, how can I initialize myin field? If I successfully cloned System.in then I will clone System.out something like Test.myout then println() method will be called with it. Later on I will write my own scanner class.. I can't do without you gentleman..Plz help me and suggest me possibilities and I will try it.. – Pushkar Jan 26 '22 at 15:14
  • @Pushkar: As @AdityaArora already said in the comments, `myin = System.in`. – Konrad Höffner Jan 26 '22 at 15:55
  • That is not working.. and more over why I use System.in... I am trying to create my own version of System.in.. it doesn't make sense to pass System.in to make another version of same. – Pushkar Jan 27 '22 at 11:21
  • It is not like instead of passing variable a I write b=a and passing b.. I don't want this.. I want to creat a independent variable b which can be passed. – Pushkar Jan 27 '22 at 11:24
  • @Pushkar: Why do you not want this when it solves your problem? Can you explain more about your specific circumstandes? This should work perfectly fine. – Konrad Höffner Jan 27 '22 at 15:12