Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points

Scanner Class In Java

what does we mean by statement Scanner scanner = new Scanner(System.in); I know that Scanner is a class and we are making its object by name "scanner" but why we are passing System.in (as System is also a class) so can we pass a class as Parameter. AND ONE THING MORE ::: What does we mean by "in" in System.in is it a variable or System is a object so we are calling a member variable.

3 Answers

Wout Ceulemans
Wout Ceulemans
17,603 Points

"in" is a public static variable. So when you do "System.in" you actually access the public static variable called "in" in the System class.

So if you would do: Scanner scanner = new Scanner(System.in);

You create a new Scanner object, store that into the scanner variable, and as a paramter you pass the public static variable "in" from the System class with it.

Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points

But In java how can we call a member variable using its class name outside the class . We should make object for that but here System is class

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

Hi Pranjal Agnihotri ,

static variables are associated with the class itself. you can call static variables outside of the class even without creating an object of that class. you just need to specify the class name first and then the name of the variable. as "System.in".

Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points

Now I understood , here "in" is a Static variable and we r using System class for its Scope resolution outside class.