static reference in a non-static method  
Author Message
TheTravellingSalesman





PostPosted: 2007-8-29 11:09:00 Top

java-programmer, static reference in a non-static method I'm trying to run a simple program that read info from a file and
displays it on the screen. However, I am constantly getting the
following error.

==================================================================================
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
Cannot make a static reference to the non-static field tokenizer
Cannot make a static reference to the non-static field tokenizer

==================================================================================

I don't want to declare any variables static since I don't think I
need to. My code is given below.

================================================================
File myFile = new File("Info.txt");
Scanner tokenizer = new Scanner(myFile);
String str = null;


public static void main(String[] args)
{


while (tokenizer.hasNextLine())
{
System.out.println(tokenizer.next());
}
}

=========================================================================

I was getting a similar error when I was trying to read in standard
input from the user i.e. using input stream reader and buffer
reader.

Can anyone please help?

 
Knute Johnson





PostPosted: 2007-8-29 11:30:00 Top

java-programmer >> static reference in a non-static method TheTravellingSalesman wrote:
> I'm trying to run a simple program that read info from a file and
> displays it on the screen. However, I am constantly getting the
> following error.
>
> ==================================================================================
> Exception in thread "main" java.lang.Error: Unresolved compilation
> problems:
> Cannot make a static reference to the non-static field tokenizer
> Cannot make a static reference to the non-static field tokenizer
>
> ==================================================================================
>
> I don't want to declare any variables static since I don't think I
> need to. My code is given below.
>
> ================================================================
> File myFile = new File("Info.txt");
> Scanner tokenizer = new Scanner(myFile);
> String str = null;
>
>
> public static void main(String[] args)
> {
>
>
> while (tokenizer.hasNextLine())
> {
> System.out.println(tokenizer.next());
> }
> }
>
> =========================================================================
>
> I was getting a similar error when I was trying to read in standard
> input from the user i.e. using input stream reader and buffer
> reader.
>
> Can anyone please help?
>

All the code in main() is static. Your Scanner, tokenizer, is an
instance variable of the class. The simplest solution is to make
tokenizer static or put its declaration inside main() and it will be static.

--

Knute Johnson
email s/nospam/knute/
 
Hal Rosser





PostPosted: 2007-8-29 12:25:00 Top

java-programmer >> static reference in a non-static method
"TheTravellingSalesman" <email***@***.com> wrote in message
news:email***@***.com...
> I'm trying to run a simple program that read info from a file and
> displays it on the screen. However, I am constantly getting the
> following error.
>
> ==================================================================================
> Exception in thread "main" java.lang.Error: Unresolved compilation
> problems:
> Cannot make a static reference to the non-static field tokenizer
> Cannot make a static reference to the non-static field tokenizer
>
> ==================================================================================
>
> I don't want to declare any variables static since I don't think I
> need to. My code is given below.
>
> ================================================================
> File myFile = new File("Info.txt");
> Scanner tokenizer = new Scanner(myFile);
> String str = null;
>
>
> public static void main(String[] args)
> {
>
>
> while (tokenizer.hasNextLine())
> {
> System.out.println(tokenizer.next());
> }
> }
>
> =========================================================================
>
> I was getting a similar error when I was trying to read in standard
> input from the user i.e. using input stream reader and buffer
> reader.
>
> Can anyone please help?

Hi
If you declare your variable "tokenizer" inside your main method, it
should resolve that error.
HTH


 
 
Andreas Leitgeb





PostPosted: 2007-8-29 18:30:00 Top

java-programmer >> static reference in a non-static method TheTravellingSalesman <email***@***.com> wrote:
> Cannot make a static reference to the non-static field tokenizer
>================================================================
> File myFile = new File("Info.txt");
> Scanner tokenizer = new Scanner(myFile);
> String str = null;

These variables are declared non-static, so they exist only
in the context of an instance of your class.

> public static void main(String[] args)
As you see, "main" *is* static (and needs to be, if you
want it to be a program's entry-point.)

> while (tokenizer.hasNextLine())
> {
> System.out.println(tokenizer.next());
> }

to solve it,
either you create an instance of your class inside main():
MyClass inst = new MyClass();
before your while-loop and then acess the tokenizer as:
inst.tokenizer

or, you do declare the tokenizer (and also the variable myFile) static,
which I'd recommend for stuff as simple as the example
(which, otoh, I guess your real task at hand isn't)

or, which would be the most elegant:
move the while-loop into a non-static method (say myMethod() )
and inside static main(), you do only: new MyClass().myMethod();

 
 
Roedy Green





PostPosted: 2007-8-29 19:48:00 Top

java-programmer >> static reference in a non-static method On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
<email***@***.com> wrote, quoted or indirectly quoted someone who
said :

>Cannot make a static reference to the non-static field tokenizer

see
http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
TheTravellingSalesman





PostPosted: 2007-8-30 9:16:00 Top

java-programmer >> static reference in a non-static method On Aug 29, 7:47 am, Roedy Green <email***@***.com>
wrote:
> On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
> <email***@***.com> wrote, quoted or indirectly quoted someone who
> said :
>
> >Cannot make a static reference to the non-static field tokenizer
>
> seehttp://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com



Thanks a lot eh!. This really helps. It works fine now