Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference a Non-static interface in a static Method

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic