*
The moose likes Beginning Java and the fly likes How to define variable of type Interface 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 "How to define variable of type Interface" Watch "How to define variable of type Interface" New topic
Author

How to define variable of type Interface

shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
Hi,

I have an assignment on hand & I can breathe easy if sumone resolves this one...
I have a simple java application using swing n stuff...
I have made my own listener interface... & compiled it. The class file for this interfac is in the same folder as my java file.
In my javafile, I have imported the package for this file, I am not implementing the interface, I am trying to define a variable of the type of my interface...& use this variable elsewhere in the code.
Now when I compile my java file, i get a compilation error saying 'cannot resolve symbol'... coz it doesnt recognize my listener interface class.
Can sumone please tell me what have I missed?

Shalaka


shalaka
SCJP
SCWCD
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Hi Shalaka,

First, please use real words. "n" and "coz" are not real words, and it makes it more difficult to read your post, and you may get less responses to your questions.

Now, on to your question. I think maybe the basic thing missing here is that an interface is just a contract. It is like the smell of a delicious curry when you are hungry. You can't eat it. But if you have some food in front of you, and it smells like a delicious curry, it is probably a curry, and hopefully it is pretty delicious.

An interface doesn't have any behavior on its own, it just says that 'if you want to be like me, you have to behave like this'. It doesn't care about the details.

So you need an Object which can say 'Yepp, I'll agree to this contract. If you call me in this way, then I will give you exactly the type of response that you expect'.
[ April 11, 2008: Message edited by: Katrina Owen ]
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
Hi Katrina,

Thank you so much for your response!

About using real words, I am quite new to java ranch so I wasnt aware that people here wouldnt be so used to these regular short forms.

About my interface issue, from your response it seems you are suggesting me to implement the interface.
Now the problem here is that I am working on somebody else's code. They made this application & its working perfect & they never implemented the interface in their code in the java file which I am talking about.
What I am asked to do is make a similar application & add some enhancements for customizations to it. So I made my java files out of their source code. The files which do not have any interdependencies have gotten compiled, but the ones like this one, where I am declaring a variable in my class of the type of interface(exactly like the original developers did), it doesnt compile.
I tried setting the class path in environment variables but thats not working.
Any suggestions please?
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
The sniplet from my code causing the error:

package com.trapi.swingapplet;

public class MyLoginPanel implements ActionListener
{
private MyLoginPanelListener m_oLoginListener;

The error message that I get:

MyLoginPanel.java:39: cannot resolve symbol
symbol : class MyLoginPanelListener

My java file as well as the compiled 'MyLoginPanelListener.class' both are in the same folder swingapplet.

Hope this gives you a clearer picture.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41017
    
  43
Did the interface class get compiled? If the code references the interface, then the compiler must either be able to find the class file of that interface, or be able to find its source to compile it. Soooo .... did the interface get compiled, and is it in the classpath when you compile the class that references it?

What's your directory structure, which command are you using for compilation, and in which directory are you when running the compiler?

Later: Just saw your response. So the first part of my answer probably doesn't apply. I think it's an issue of the directory from where you run the compiler - which one is it? Is it the one that contains the "com" directory in which all source code and the class files reside?
[ April 12, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
Thanks..

Yes my listener inferface is compiled. Both my source code as well as the compiled listener interface class are inside: C:\SampleApi\com\trapi\swingapplet (ie both are in the same package)
Do I still need to define the classpath. If yes, how & where?
Also I may sound silly if I ask this, but I will.. I am not using any server for my application..hence may be no container to find my classes. Is that the problem? Or is it that this application can be worked up on without any server?

Thanks in advance,
Shalaka
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41017
    
  43
During compilation, all dependent classes need to be accessible. If we knew form which directory you're compiling, and which command you're using, we might point you in the right direction.

It sounds as if this is a GUI application, so I don't see what you would need a server for. Or what do you mean by "container"? If it's an applet then you will need a web server to serve it eventually, but not for compilation.
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
Hi Ulf,

Thank you so much for answering my query!

When you ask which directory I am compiling from, what do you exactly mean. Because if you mean package or my folder structure, I have already mentioned it as: package com.trapi.swingapplet
Both my Java file which i am trying to compile as well as the class file which I am trying to access are in the same folder ie 'swingapplet'
I am using the javac command to compile.

Regards,
Shalaka
[ April 14, 2008: Message edited by: shalaka wadekar ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41017
    
  43
The question was about in which directory you are when you type in the javac command. If you are in the SampleApi directory, you would need to type "javac com\trapi\swingapplet\MyApplet.java" to compile, and the compiler would automatically find the interface, because you are at the root of the package hierarchy.

But if you are in the swingapplet directory and type "javac MyApplet.java" it won't find the interface class. You'd need to use either "javac -cp C:\SampleApi MyApplet.java" or "javac *.java".
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
Hi Ulf,

I have been trying to compile from the swingapplet directory using javac MyApplet.java.
After getting your reply, I tried to compile from the SampleApi directory using javac com\trapi\swingapplet\MyApplet.java. But this didnt work, it gives the same error "cannot resolve symbol.. class: MyLoginPanelListener"
I also tried the other options you suggested, for compiling from the swingapplet directory. But my compiler says -cp is an invalid flag. Doing "javac *.java" gives the same error as before ie "cannot resolve symbol.. class: MyLoginPanelListener"


Regards,
Shalaka
[ April 15, 2008: Message edited by: shalaka wadekar ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41017
    
  43
You can use "-classpath" instead of "-cp".

Just to make sure I understand - you are in directory swingapplet (which contains both the applet class and the class the error says is missing), and if you type "javac *.java" is still complains about missing classes? That's very odd indeed.
shalaka wadekar
Ranch Hand

Joined: Mar 21, 2008
Posts: 37
Yes you are absolutely right..

I have already tried -classpath & set CLASSPATH...
My src has a package declaration... Is that the problem?

Regards,
Shalaka
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to define variable of type Interface
 
Similar Threads
Encapsualtion
Incrementing by 1
Custom Events, Listeners, & Performance
Java File Reading
IS-A / HAS-A relationships. Chapter 2 Question 14 page 180