non-static variable this cannot be referenced from a static context

Discussion in 'Java' started by kookey, Aug 20, 2005.

  1. kookey

    kookey Guest

    Today i always get this error:
    non-static variable xxx cannot be referenced from a static context
    ^
    e.g.
    import java.net.URL;
    class Test{
    public static void main(String[] args){
    URL url = this.getClass().getResource("1.xml");
    System.out.println(url);
    }
    }
    error: non-static variable this cannot be referenced from a static
    context ^

    But,when
    import java.net.URL;
    class Test{
    public Test(){
    URL url = this.getClass().getResource("1.xml");
    System.out.println(url);
    }
    public static void main(String[] args){
    Test test = new Test();
    }
    }
    The result is right.
    I want why.

    >kookey<
    kookey, Aug 20, 2005
    #1
    1. Advertising

  2. kookey

    Joan Guest

    "kookey" <> wrote in message
    news:...
    > Today i always get this error:
    > non-static variable xxx cannot be referenced from a static
    > context
    > ^
    > e.g.
    > import java.net.URL;
    > class Test{
    > public static void main(String[] args){
    > URL url = this.getClass().getResource("1.xml");
    > System.out.println(url);
    > }
    > }
    > error: non-static variable this cannot be referenced from a
    > static
    > context ^
    >
    > But,when
    > import java.net.URL;
    > class Test{
    > public Test(){
    > URL url = this.getClass().getResource("1.xml");
    > System.out.println(url);
    > }
    > public static void main(String[] args){
    > Test test = new Test();
    > }
    > }
    > The result is right.
    > I want why.
    >
    >>kookey<


    main() is static, Test() is not static.
    Joan, Aug 20, 2005
    #2
    1. Advertising

  3. kookey

    kookey Guest

    why "a non-static variable cannot be referenced from a static context"
    ?
    please say clearly , the 2nd floor - Joan.
    kookey, Aug 20, 2005
    #3
  4. On 20 Aug 2005 11:55:08 -0700, kookey wrote:

    > please say clearly ,..


    Detailed answers are best found on a different group..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "I have to turn my head, until my darkness goes"
    The Rolling Stones 'Paint It Black'
    Andrew Thompson, Aug 20, 2005
    #4
    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. Gary
    Replies:
    1
    Views:
    3,956
    Ryan Stewart
    Jan 16, 2004
  2. Michael
    Replies:
    3
    Views:
    4,918
    Adam Jenkins
    Jan 27, 2004
  3. G. Burton
    Replies:
    2
    Views:
    59,869
    Sudsy
    Feb 21, 2004
  4. Johannes Beekhuizen
    Replies:
    14
    Views:
    1,323
    John B. Matthews
    Aug 5, 2005
  5. Johannes Beekhuizen
    Replies:
    0
    Views:
    1,523
    Johannes Beekhuizen
    Aug 5, 2005
Loading...

Share This Page