wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes non-static methos cannot be referenced from static context? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "non-static methos cannot be referenced from static context?" Watch "non-static methos cannot be referenced from static context?" New topic

non-static methos cannot be referenced from static context?

samantha katam

Joined: Sep 19, 2005
Posts: 16
Hi everyOne,

I am getting:
java:830: non-static method createDocument(java.io.InputS
tream,boolean,boolean) cannot be referenced from a static context
error when i try to compile the code. Can any one tell me why it sbeing caused and how do i go about correcting it. The error is occuring near EOXMLUtils.createDocument

public static Document getDoc(InputStream in, boolean validate)
throws SAXException, IOException, EOXMLParseException
Document document = null;

// Build a DOM Document from the InputStream
logDebug( "in try for getDoc" );
document = EOXMLUtils.createDocument(in, validate, USE_NAMESPACE);
catch (EOXMLParseException e)
logDebug("Parse error occurred: EOXMLParseException");
throw e;
catch (FileNotFoundException e)
if (validate)
{ throw e; }
logDebug("\n---> FileNotFoundException caught in "+
if (in != null)
{ in.close(); }
catch (IOException e)
{ e.printStackTrace(); }

return document;
marc weber

Joined: Aug 31, 2004
Posts: 11343

It appears that you need to either create an instance of EOXMLUtils and call the method using that instance, or declare the method static within EOXMLUtils. (The second option seems more likely for a "utility" class.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37923
This is a general problem. Whenever you declare a method static, it applies without change to the class, the whole class and nothing but the class.
So, whichever object you are coming from, it is exactly the same. So you can call a static method from an object ("non-static"), but not vice versa.

If you call an instance method or instance variable ("non-static") from a static method, the static method cannot tell how many objects there might or might not be. So, the compiler will complain. Otherwise, if there are several objects of the class, the static method might call the non-static method on the wrong object.

Usually whenever I have had that compiler error, I have been trying to access an instance member of the same class from its static methods. So is your method a member of EOXMUtils?
I agree with Marc Weber that you can create an instance of the class from within a static method (after all, that is what you do if you have a main method within the same class which does something), or set the method you are calling static.
samantha katam

Joined: Sep 19, 2005
Posts: 16
Thanks guys !

It really helped me out. Well i created a instance for EOXMLUtils.
wood burning stoves
subject: non-static methos cannot be referenced from static context?
Similar Threads
Write a program that reads and writes from binary or text files
Help with OCSP Stand Alone Implementation
Loading resource in static method
Proxy again...Authentication problem...
Connection timed out while downloading