non-static method cannot be referenced from a static context

M

mstng_67

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: (e-mail address removed) (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

Banne said:
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
 
A

Andrew Thompson

mstng_67 said:
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.
 
C

castillo.bryan

mstng_67 said:
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: (e-mail address removed) (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

Banne said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top