non-static method cannot be referenced from a static context

Discussion in 'Java' started by mstng_67, Dec 20, 2006.

  1. mstng_67

    mstng_67 Guest

    I have read posts on this group dealing with this syntax error. My
    trouble goes a little further than that of needing to instantiate an
    object as described below in a previous post. I am trying to invoke
    Class.getResource(String arg) and am getting the same syntax error.
    However, I am not able to instantiate a Class object as there are no
    constructors for class Class. The Java Virtual Machine creates the
    objects automatically. The following is my code. Pasted after that is
    the post advising someone to instantiate an object. Can anyone help?

    import java.io.*;
    import java.net.*;

    public class Driver
    {
    //##################### methods #########################
    static void printPath()
    {
    URL myURL = Class.getResource(".");
    System.out.println(myURL.toString());
    }//end of printPath

    public static void main( String[] args) throws Exception
    {
    //################### logic for main ####################
    printPath();

    } //===== end of main =====

    } //======= end of class Driver =======


    From: Lee Weiner - view profile
    Date: Fri, Nov 22 2002 11:16 pm
    Email: (Lee Weiner)
    Groups: comp.lang.java.programmer
    Not yet rated
    Rating:
    hide options
    Reply | Reply to Author | Forward | Print | Individual Message | Show
    original | Report Abuse | Find messages by this author

    In article <>, Banne
    <> wrote:

    >I got the following error when I compiled my program:


    >non-static method createImage(int,int) cannot be referenced from a
    >static context
    > Image img = Frame.createImage(2000,1000);


    >I am sure that I called the method within a non-static method, but
    >still it complained about calling it within a static method.
    >Anyone knows why?


    Not within a static method, within a static context. The message is
    telling
    you that createImage() is a non-static method and you're calling it
    from the
    name of the class, Frame. Non-static methods have to be called from
    the name
    of an instantiated object:
    Frame frm = new Frame(...);
    Image img = frm.createImage(2000, 1000);

    Lee Weiner
    lee AT leeweiner DOT org
    mstng_67, Dec 20, 2006
    #1
    1. Advertising

  2. mstng_67 wrote:
    > I have read posts on this group dealing with this syntax error. My
    > trouble goes a little further than that of needing to instantiate an
    > object as described below in a previous post. I am trying to invoke
    > Class.getResource(String arg) and am getting the same syntax error.
    > However, I am not able to instantiate a Class object as there are no
    > constructors for class Class.

    .....
    > import java.io.*;
    > import java.net.*;
    >
    > public class Driver
    > {
    > //##################### methods #########################
    > static void printPath()
    > {
    > URL myURL = Class.getResource(".");


    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#getClass()>

    Andrew T.
    Andrew Thompson, Dec 20, 2006
    #2
    1. Advertising

  3. mstng_67

    Guest

    mstng_67 wrote:
    > I have read posts on this group dealing with this syntax error. My
    > trouble goes a little further than that of needing to instantiate an
    > object as described below in a previous post. I am trying to invoke
    > Class.getResource(String arg) and am getting the same syntax error.
    > However, I am not able to instantiate a Class object as there are no
    > constructors for class Class. The Java Virtual Machine creates the
    > objects automatically. The following is my code. Pasted after that is
    > the post advising someone to instantiate an object. Can anyone help?
    >
    > import java.io.*;
    > import java.net.*;
    >
    > public class Driver
    > {
    > //##################### methods #########################
    > static void printPath()
    > {
    > URL myURL = Class.getResource(".");


    Try this:

    URL myURL = Driver.class.getRsource(".");


    > System.out.println(myURL.toString());
    > }//end of printPath
    >
    > public static void main( String[] args) throws Exception
    > {
    > //################### logic for main ####################
    > printPath();
    >
    > } //===== end of main =====
    >
    > } //======= end of class Driver =======
    >
    >
    > From: Lee Weiner - view profile
    > Date: Fri, Nov 22 2002 11:16 pm
    > Email: (Lee Weiner)
    > Groups: comp.lang.java.programmer
    > Not yet rated
    > Rating:
    > hide options
    > Reply | Reply to Author | Forward | Print | Individual Message | Show
    > original | Report Abuse | Find messages by this author
    >
    > In article <>, Banne
    > <> wrote:
    >
    > >I got the following error when I compiled my program:

    >
    > >non-static method createImage(int,int) cannot be referenced from a
    > >static context
    > > Image img = Frame.createImage(2000,1000);

    >
    > >I am sure that I called the method within a non-static method, but
    > >still it complained about calling it within a static method.
    > >Anyone knows why?

    >
    > Not within a static method, within a static context. The message is
    > telling
    > you that createImage() is a non-static method and you're calling it
    > from the
    > name of the class, Frame. Non-static methods have to be called from
    > the name
    > of an instantiated object:
    > Frame frm = new Frame(...);
    > Image img = frm.createImage(2000, 1000);
    >
    > Lee Weiner
    > lee AT leeweiner DOT org
    , Dec 20, 2006
    #3
  4. Thomas Hawtin, Dec 20, 2006
    #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,961
    Ryan Stewart
    Jan 16, 2004
  2. Michael
    Replies:
    3
    Views:
    4,924
    Adam Jenkins
    Jan 27, 2004
  3. G. Burton
    Replies:
    2
    Views:
    59,874
    Sudsy
    Feb 21, 2004
  4. Johannes Beekhuizen
    Replies:
    14
    Views:
    1,327
    John B. Matthews
    Aug 5, 2005
  5. Johannes Beekhuizen
    Replies:
    0
    Views:
    1,526
    Johannes Beekhuizen
    Aug 5, 2005
Loading...

Share This Page