aspose file tools*
The moose likes Beginning Java and the fly likes Error message relating to Static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error message relating to Static variables" Watch "Error message relating to Static variables" New topic
Author

Error message relating to Static variables

Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
The error is; "non-static variable allValuesScanner cannot be referenced from a static context." I'll type an arrow to make the line more visible. This is just the very start of the program, but this is an error I've never heard of. Any fix would be greatly appreciated, but I would much rather learn more about what this error means, and how to fix it than get a "Change this line to such-and-such." I know this could be a lot to ask, but you guys have been GREAT in the past. Many thanks.

Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

there are three errors in your code:

line 12 - import static java.lang.System.out; // compiler

line 25 - out.println("Input lowest variable") ; // compiler

skip your line 12 and change #25 to "System.out.println("Input lowest variable");"

re number three:

you can fix it if you change line 27 to "static Scanner allValuesScanner = new Scanner(System.in);"

the reason for that error is that your 'main' method is static (and it has to be).
From a static method in your class you only can access static members.

But I think what you really wanna do is something like this:



would make it easier to extract code into other classes and have the AllValuesRandomizer called from an external main...

You could change that main method to:


HTH
Matt
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
OK. I really appreciate yout taking the time to help me out. I need to ask a few questions though to flesh out your answer for my understanding.

What does it mean for a method or a member to be "static"?

When I just say "Scanner allValuesScanner = new Scanner....." if it's not "static" what is it?

What is the error in line 12?

What did you mean by "//compiler"?

I'm very new. So I need a lot more definitions than I should. I haven't had time to learn all the terms yet, obviously. I am sure that there is a lot of great information here, and thank you again for that. I am just trying to do a better job understanding it.



Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

What is the error in line 12?

What did you mean by "//compiler"?

Apologies, it is valid 1.5 (and later) syntax, my compiler (HotSpot 1.4 on sun4u) complained and I didn't double check.

When I just say "Scanner allValuesScanner = new Scanner....." if it's not "static" what is it?



creates a new instance of 'Scanner'

What does it mean for a method or a member to be "static"?

you can call static methods from an instance or static context, but you cannot access instance members or
methods from a static context without creating an instance within that static context first (more here and here).

HTH
Matt
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

Michael Rippee wrote:What does it mean for a method or a member to be "static"?

It means that the member variable is a class member rather than an instance member; there's only one copy of the member variable that is shared by all instances of the class, rather than each instance having its own copy.

See: Understanding Instance and Class Members.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Ok, awesome. Thanks a lot! Last two questions so that I an learn as much as possible.

What does it mean to "call" a variable? (I think I know but it's a basic concept so I should really be sure before I move on)

Also, could someone give me a basic example of a static variable and an example of an instance variable? Is it something like, In the class Bicycle there are variables for cadence, numberOfGearTeeth, speed, numberOfWheels, and chainLength. numberOfWheels is a class variable because, by definition, all Bicycle objects have two wheels, whereas they might, and probably will, have different instances of speed, cadence, etc. Is that basically what we're talking about here?


What I did was change;



to



Making my Scanner static. Why can you not reference instance variables from a static context? I know it doesn't really matter why, so long as I can use it, but it would help me to remember if someone does know why.
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

Making my Scanner static.

negative, that doesn't make the scanner static.
You are creating an instance of Scanner in a static context.
Your local variable 'allValuesScanner' in the same static context references that instance of Scanner.
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Aha! Thanks. I still need to be sure I'm getting the difference in static and class variables, as well as what it means to call a variable. I'll restate my questions here. I'm loving the information, so thanks again to everyone helping out!

What does it mean to "call" a variable? (I think I know but it's a basic concept so I should really be sure before I move on)

Also, could someone give me a basic example of a static variable and an example of an instance variable? Is it something like, In the class Bicycle there are variables for cadence, numberOfGearTeeth, speed, numberOfWheels, and chainLength. numberOfWheels is a class variable because, by definition, all Bicycle objects have two wheels, whereas they might, and probably will, have different instances of speed, cadence, etc. Is that basically what we're talking about here?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Michael Rippee wrote:What does it mean to "call" a variable? (I think I know but it's a basic concept so I should really be sure before I move on)


It doesn't mean anything. If you saw somebody using that terminology, it's because they are a beginner and don't know the words for things. You can call a method (you know what that means, right?) but calling a variable doesn't mean anything.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Michael Rippee wrote:Also, could someone give me a basic example of a static variable and an example of an instance variable? Is it something like, In the class Bicycle there are variables for cadence, numberOfGearTeeth, speed, numberOfWheels, and chainLength. numberOfWheels is a class variable because, by definition, all Bicycle objects have two wheels, whereas they might, and probably will, have different instances of speed, cadence, etc. Is that basically what we're talking about here?


Yes, that's pretty close. Not a perfect example, but then it's hard to come up with good examples. The majority of classes won't have any static variables.
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Awesome. Thanks.

I know what it means to call a meathod if it's something like;

Class.method(paramater)
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

alrighty, let's talk some more code

Let's say we want to know for each instance of a class how many instances have been initialized.
So we count them. To get there , we implement a NumberedObject that encapsulates a counter
and an instance number.



Then we create two instance of this class and invoke (call) two of their methods:


Hey, we did not code any getClass() method in our NumberedObject, where the heck is that coming form?

HTH
Matt
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
I don't know. I'm guessing it's just API.

Thanks for the great example. something I'm working on is referencing and calling methods from other classes, and this is a great example of that.

What is; doing in this code? I get that the end result is to display the name of the class, but I'm not sure how that piece of code is doing that.
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

Michael Rippee wrote:Thanks for the great example. something I'm working on is referencing and calling methods from other classes, and this is a great example of that.

You're welcome.

Michael Rippee wrote:I don't know. I'm guessing it's just API.

Sorry, wrong guess.

What is the super class of com.coderanch.examples.sni.NumberedObject?

And what does it implement?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error message relating to Static variables