• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent: please help: importing variables interdependent classes

 
Abhi Chat
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im tryin to use a variable of class x in class y, and vice versa. they are in the same package. Now the problem is i cacnot compile claas x as it says variable not found and niether class y as it gives the same error.

My problem is : i have 2 class x which is the GUI and classs Y which does the prser of the file and gives result. Class Y needs the name of the file which the user accepts through JFIleChooser, and does the work. Now class X calls the method of class y to get results. Now i cant compile class y as it needs the variable from class X and neither can class X as it needs the method from class Y.

Please advice

thanx
abhi
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhi,

Perhaps you could provide a simple example so we can see where the problem lies?

For example, I threw together these two classes as an example. There is no real GUI - just a simulation, but you should be able to see that it all works:

X.java

Y.java

Compiling them

And then running:

So this is one example of how all this works.

Normally you would not want to do compilations in this way - it would be much more sensible to have two directories, one for source and one for output. Then just tell javac to compile everything in the source directory and put it in the output directory. Then you would start looking at having your classes in packages, and getting the compilations working. Then when you get sick of all the commands you are typing (and retyping), you would probably want to look at a build tool ('ant' for example).

Anyway - what I did was quick and dirty to give you an example of how to give us an example of the problem. If you can write some simple code that demonstrates your problem, we may be able to help you.

Regards, Andrew
 
Abhi Chat
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..heres what i did

This is the supporting class...it needs to have a String variable
that is the String filename which it uses to do some analysis

public class analysis
{

FileReader rd;
FileWriter phy;
FileWriter gen;
BufferedReader brd;

JFileChooser fc;
File file;

public void methodAnalysis()
{
try
{
rd=new FileReader(fileName);
brd = new BufferedReader(rd);

phy =new FileWriter("physical.txt");
gen =new FileWriter("genetic.txt");

}
catch(FileNotFoundException e3)
{
System.out.println(e3.toString());
//System.exit(0);
}

catch(IOException e4)
{
System.out.println(e4.toString());
//System.exit(0);
}

.....................................................

}

This class has the main and the gui....it accepts the file name through
Jfilechooser and then has to call the above analysis class to generate 2 files....and then again calls its method to work on those 2 files...

public void actionPerformed(ActionEvent e)
{

// Analysis of the loaded file and display output
if(e.getSource()== headerB1)
{
anaD = new analysis();
// this belongs to the class analysis
anaD.methodAnalysis();
// this belongs to this class
methodWrite();

}

I tried doing what you did but in vain..so im thinkin of including the analyis class as an inner class....

Please advice
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhi,

If you look at my post earlier, you will see that I put my code inside UBB [code] blocks. Doing so makes the code easier to read, since formating is preserved. It would be helpful if you did the same.

In your "analysis" class, you have the line but you have not specified where "filename" is defined. Assuming it is defined in the "main and gui" class, you could try passing it as a parameter to the "methodAnalysis()" method. That way it becomes a local variable to methodAnalysis().

You don't seem to use the following definitions in the "analysis" class: Perhaps you should remove them?

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic