Scanner keyboard= new Scanner(System.in);
int answer= keyboard.nextInt();
In the above code a Scanner object is created in the heap memory and its reference is stored in a variable called keyboard(keyboard varible is stored in stack memory). Using variable keyboard you can able access the Scanner object at any point of the program.
int answer= new Scanner(System.in).nextInt();
In the second statement you are creating object which is also stored in heap memory , but the reference of the object is not stored in any variable. So you cannot able to access this object anymore. After this statement the object in heap memory are ready to be garbage collected , since its reference is not used anymore.