This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Why cant both my classes see my scanner?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two classes,

and in the class with my main method I have this



but why can none of my methods in the other class see my sc scanner?

 
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should show the code for those other classes so we know exactly what it is you're trying to do. See also "It doesn't work." is useless -- TellTheDetails (←that's also a link)
 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to fix it by going

Shop.sc.nextInt();

is that normal? i never thought I did that in the past, thought sc would just work cause its public.
 
Junilu Lacar
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, setting up a public static variable like that so that anyone and your mother's brother's cousin's father's friend from high school can get to it is NOT a good practice. Try to keep the scope of variables as small as possible, so you maintain control on what happens to it. Otherwise, it's a free-for-all and you can run into some pretty nasty problems with that.

See why global static variables should be avoided
 
Junilu Lacar
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By naming the fully qualified name of the variable, you gave Java all the information it needed to locate it. You could have also fixed it by adding a static import of Shop.sc, not that it would be a good practice to do (see my last reply).

 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!