static reference in a non-static method

Discussion in 'Java' started by TheTravellingSalesman, Aug 29, 2007.

  1. 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?
     
    TheTravellingSalesman, Aug 29, 2007
    #1
    1. Advertising

  2. 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/
     
    Knute Johnson, Aug 29, 2007
    #2
    1. Advertising

  3. TheTravellingSalesman

    Hal Rosser Guest

    "TheTravellingSalesman" <> wrote in message
    news:...
    > 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
     
    Hal Rosser, Aug 29, 2007
    #3
  4. TheTravellingSalesman <> 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();
     
    Andreas Leitgeb, Aug 29, 2007
    #4
  5. TheTravellingSalesman

    Roedy Green Guest

    Roedy Green, Aug 29, 2007
    #5
  6. On Aug 29, 7:47 am, Roedy Green <>
    wrote:
    > On Wed, 29 Aug 2007 03:08:42 -0000, TheTravellingSalesman
    > <> 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
     
    TheTravellingSalesman, Aug 30, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,698
    cgbusch
    Jul 5, 2003
  2. Gary
    Replies:
    1
    Views:
    4,026
    Ryan Stewart
    Jan 16, 2004
  3. Michael
    Replies:
    3
    Views:
    4,958
    Adam Jenkins
    Jan 27, 2004
  4. William Colls
    Replies:
    4
    Views:
    2,058
    Roedy Green
    Mar 20, 2012
  5. rickman
    Replies:
    5
    Views:
    434
    rickman
    Mar 30, 2013
Loading...

Share This Page