0

Could someone explain to me when I run this code, I don't get the Sysout statement until I enter my first keyboard input?

    import java.util.Scanner;

    public class test1{

            static Scanner scan = new Scanner(System.in);
            static int k = scan.nextInt();

            public static void main(String[] args) {
                setK();
                System.out.println(" K is: " + k);
        }
            public static void setK(){
                System.out.println("Please input K value");
                k = scan.nextInt();
        }
    }
Bacteria
  • 8,406
  • 10
  • 50
  • 67
Anthony J
  • 551
  • 1
  • 9
  • 19
  • Possible duplicate of [when static variables are initialized in Java](http://stackoverflow.com/questions/8704423/when-static-variables-are-initialized-in-java) – Julien Lopez Dec 15 '16 at 13:19

3 Answers3

3

The static variables of your test1 class are initialized before your main method is executed. This happens when the class is initialized.

Therefore the

static int k = scan.nextInt();

statement is executed before your main method and waits for input. Only after the input is entered, main starts running and calls setK();, which prints "Please input K value".

I'm not sure this was intentional, since your setK() method seems to be the method that should read the input and assign it to k. Therefore, change your code to :

import java.util.Scanner;

public class test1{

        static Scanner scan = new Scanner(System.in);
        static int k;

        public static void main(String[] args) {
            setK();
            System.out.println(" K is: " + k);
    }
        public static void setK(){
            System.out.println("Please input K value");
            k = scan.nextInt();
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

This line

static int k = scan.nextInt();

runs during class initialization. It blocks and waits for input of an integer.

This code runs before main because it is a static initialization. It must be complete before the first method of the class is called. At that point k has the first value you have entered. After that, main calls setK, prompting for another input.

You can fix this by removing initialization (i.e. the = scan.nextInt(); part) from declaration of k.

Winter
  • 3,894
  • 7
  • 24
  • 56
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Maybe the behaviour you expect would be as follows:

package test;

import java.util.Scanner;

public class ScannerTest {

    static Scanner scan = new Scanner(System.in);
    static int k;

    public static void main(String[] args) {
        System.out.println("Please input K value");
        k = scan.nextInt();
        System.out.println(" K is: " + k);
    }
}

BTW, you should stick to Java naming conventions.

Blablalux
  • 485
  • 1
  • 5
  • 9
  • Could you explain more by what you mean as Java naming conventions? – Anthony J Dec 15 '16 at 13:23
  • @911DidBush, ahh thanks. I thought I was doing something wrong so I asked. I've had a read and will try to go along with those - thank you. – Anthony J Dec 15 '16 at 13:27
  • Your title mention "functions". Since Java 8, the "function" term refers to the target for a lambda expression or method reference (see [link](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html)). And the name of your class is supposed to be mixed-case (see [link](http://www.oracle.com/technetwork/java/codeconventions-135099.html)) – Blablalux Dec 15 '16 at 13:29