Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

static reference in a non-static method

Thread view: 
TheTravellingSalesman - 29 Aug 2007 04:08 GMT
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 - 29 Aug 2007 04:29 GMT
> 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
[quoted text clipped - 32 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Hal Rosser - 29 Aug 2007 05:24 GMT
> 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
[quoted text clipped - 32 lines]
>
> Can anyone please help?

Hi
 If you declare your variable "tokenizer" inside your main method, it
should resolve that error.
HTH
Andreas Leitgeb - 29 Aug 2007 11:29 GMT
>     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 - 29 Aug 2007 12:47 GMT
On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
<saad.zaman@gmail.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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

TheTravellingSalesman - 30 Aug 2007 02:16 GMT
On Aug 29, 7:47 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
> <saad.za...@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 6 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.