← Back to Week 1 Hub

Scanner in Multiple Methods

Workbook 1c, p.112-115 — Three approaches compared

✗ BAD — Avoid This

Code

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name = getName(); // no Scanner passed! int age = getAge(); // no Scanner passed! System.out.println("Hello " + name + ", age " + age); } public static String getName() { Scanner scanner = new Scanner(System.in); // NEW Scanner! System.out.println("Enter name: "); return scanner.nextLine(); } public static int getAge() { Scanner scanner = new Scanner(System.in); // ANOTHER new Scanner! System.out.println("Enter age: "); return scanner.nextInt(); }
  • Creates 3 separate Scanner objects all reading from the same System.in
  • Wasteful — only one stream, so multiple Scanners fight over it
  • Can cause mysterious bugs: data consumed by one Scanner is gone for others

How It Looks at Runtime

main()
Scanner #1
new Scanner(System.in)
getName()
Scanner #2
new Scanner(System.in)
getAge()
Scanner #3
new Scanner(System.in)
System.in (single stream)

Three Scanners all competing for one input stream

∼ OK — Works, But Not Preferred

Code

static Scanner scanner = new Scanner(System.in); // class-level field public static void main(String[] args) { String name = getName(); int age = getAge(); System.out.println("Hello " + name + ", age " + age); } public static String getName() { System.out.println("Enter name: "); return scanner.nextLine(); // uses the shared field } public static int getAge() { System.out.println("Enter age: "); return scanner.nextInt(); // uses the shared field }
  • Only one Scanner exists — good!
  • But it is a static field (class-level state)
  • Methods don't declare that they need a Scanner — hidden dependency
  • When we learn OOP, we'll see why global-ish state causes problems

How It Looks at Runtime

Scanner (static field)
new Scanner(System.in)
main()
getName()
getAge()

Every method implicitly reaches up to the static field.
Nothing in the method signature says "I need a Scanner."

System.in (single stream)

One Scanner, one stream — no conflict, but the coupling is invisible

✓ PREFERRED

Code

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // created ONCE String name = getName(scanner); // passed in! int age = getAge(scanner); // passed in! System.out.println("Hello " + name + ", age " + age); } public static String getName(Scanner scanner) { System.out.println("Enter name: "); return scanner.nextLine(); } public static int getAge(Scanner scanner) { System.out.println("Enter age: "); return scanner.nextInt(); }
  • Only one Scanner — created in main()
  • Each method's signature explicitly says it needs a Scanner
  • Easy to test: you can pass a different Scanner (e.g., from a file)
  • No hidden state — every dependency is visible

How It Looks at Runtime

main()
Scanner scanner = new Scanner(System.in)
passes scanner
passes scanner
getName(scanner)
getAge(scanner)
ONE Scanner object
shared via parameters
System.in (single stream)

One Scanner, one stream — and the relationship is explicit

Key Takeaway

Create Scanner once in main(), then pass it to every method that needs it.

This pattern applies to any shared resource, not just Scanner — database connections, file readers, etc.

Tip: If a method needs something to do its job, that something should be a parameter.

Workbook 1c • p.112-115

← Method Decomposition switch Statement Flow →