Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - non-static method cannot be referenced from a static context

 
Thread Tools Search this Thread
Old 12-20-2006, 04:52 PM   #1
Default non-static method cannot be referenced from a static context


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: l...@NOSPAM.com (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 <v8nttuk8ldg2irss2o0unn49lh3llf9...@4ax.com>, Banne
<a...@nomorespemmingc.bb> 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
  Reply With Quote
Old 12-20-2006, 05:03 PM   #2
Andrew Thompson
 
Posts: n/a
Default Re: non-static method cannot be referenced from a static context
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
  Reply With Quote
Old 12-20-2006, 05:16 PM   #3
castillo.bryan@gmail.com
 
Posts: n/a
Default Re: non-static method cannot be referenced from a static context

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: l...@NOSPAM.com (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 <v8nttuk8ldg2irss2o0unn49lh3llf9...@4ax.com>, Banne
> <a...@nomorespemmingc.bb> 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




castillo.bryan@gmail.com
  Reply With Quote
Old 12-20-2006, 05:19 PM   #4
Thomas Hawtin
 
Posts: n/a
Default Re: non-static method cannot be referenced from a static context
Andrew Thompson wrote:
> mstng_67 wrote:
>> URL myURL = Class.getResource(".");

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


Or more appropriately, "15.8.2 Class Literals":

http://java.sun.com/docs/books/jls/t...ns.html#15.8.2

Tom Hawtin


Thomas Hawtin
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
please help: simple java coding error 'cannot be referenced from a static context' clm90 General Help Related Topics 0 10-17-2009 06:49 AM
Cancelling Timer and TimerTask threads wfalby Software 1 04-30-2009 01:04 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46