aspose file tools*
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
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
Author

non-static methos cannot be referenced from static context?

samantha katam
Greenhorn

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;

try
{
// 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; }
else
{
e.printStackTrace();
logDebug("\n---> FileNotFoundException caught in "+
"CSSSDKTestSupport.getDoc()\n");
}
}
finally
{
try
{
if (in != null)
{ in.close(); }
}
catch (IOException e)
{ e.printStackTrace(); }
}

return document;
}
marc weber
Sheriff

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
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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
Greenhorn

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

It really helped me out. Well i created a instance for EOXMLUtils.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: non-static methos cannot be referenced from static context?