Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

non-static method cannot be referenced from a static context

Thread view: 
mstng_67 - 20 Dec 2006 16:52 GMT
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
Andrew Thompson - 20 Dec 2006 17:03 GMT
> 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.*;
[quoted text clipped - 5 lines]
>    {
>       URL myURL = Class.getResource(".");

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

Andrew T.
Thomas Hawtin - 20 Dec 2006 17:19 GMT
>>       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/third_edition/html/expressions.html#15.8.2

Tom Hawtin
castillo.bryan@gmail.com - 20 Dec 2006 17:16 GMT
> 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
[quoted text clipped - 14 lines]
>    {
>       URL myURL = Class.getResource(".");

Try this:

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

>       System.out.println(myURL.toString());
>    }//end of printPath
[quoted text clipped - 43 lines]
> Lee Weiner
> lee AT leeweiner DOT org


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.