Can I have macros in Java source files

后端 未结 8 2527
情话喂你
情话喂你 2020-11-28 03:15

In my program I\'m reading integers form console many times. Every time, I need to type this line.

new Scanner(System.in).nextInt(); 

I\'m

8条回答
  •  囚心锁ツ
    2020-11-28 03:53

    There is no macro concept in Java. If you're doing that a lot, it's a bad idea to instantiate a new Scanner each time. Define a public static Scanner then reuse it each time:

    public class YourClass {
      public static final Scanner SCANNER= new Scanner(System.in);
      ...
    }
    
    // Somewhere in your code
    YourClass.SCANNER.nextInt();
    

提交回复
热议问题