jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Reference a Non-static interface in a static Method 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 » Java in General
Bookmark "Reference a Non-static interface in a static Method" Watch "Reference a Non-static interface in a static Method" New topic
Author

Reference a Non-static interface in a static Method

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have a bean class, which accesses a data access object interface and the concrete class that implements that interface; therefore, I have a statement like:

There is a static method in this bean class. I got a compilation saying that the instance with the type of the data access object interface cannot be referenced from a static context:

EditorBean.java:42: non-static variable ed cannot be referenced from a static context
return ed.findEditor( username );

What should I do?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Your 2 choices are to
1. Make the method non-static ( instance )
2. Make the EditorDAO attribute static
depends on which one makes sense.
Otherwise, say you have

now which ed object is the findEditor( username ); going to be called from? the one from b1 or the one from b2? that's why the java compiler is complaining.
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

but on further inspection, I wondered why you passed an EditorBean into the findEditorData() method when you don't ever use it. So I think that you may want to reference the parameter EditorBean's ed object. You just have to change your code to say that you want to use the ed object from the EditorBean parameter:

Now the compiler knows which ed object you want to call the findEditor method on.
Jamie
[ November 28, 2003: Message edited by: Jamie Robertson ]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks for your reply. I deleted the second parameter in the findEditorData(); But, I am still not out of the woods. Now, please help me with my conceptual problem:
1. I got "non-static variable ed cannot be referenced from a static context"

when my code is

The error message was the same after I changed to

2. I then made findEditorData( String username ) non-static

I got this compilation error:

[ November 28, 2003: Message edited by: JiaPei Jen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference a Non-static interface in a static Method
 
Similar Threads
How To Create This Drop-Down List
Populate JavaBean With Data Fetched From the Database
OneToOne persisting two objects in a single transaction
Struggling With Passing Data Between Data Access Class, Business Bean, and Controller
NumberFormatException while inserting data into database