aspose file tools*
The moose likes Java in General and the fly likes Importing variables interdependent classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Importing variables interdependent classes" Watch "Importing variables interdependent classes" New topic
Author

Importing variables interdependent classes

Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
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

[ EJFH: Removed "URGENT". All questions are equally important here. ]
[ October 17, 2004: Message edited by: Ernest Friedman-Hill ]

SCJP 1.4<br />Preparing for <br />SCJD and SCWCD
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Compile them both at once:

javac X.java Y.java


[Jess in Action][AskingGoodQuestions]
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
hi i tried doin it but it didnt work....it stil gives the error that variable in file x is missin

please advice
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please post your code? (Be sure to use the UBB CODE tags to preserve your formatting. There are buttons below the message editing box.) I suspect there is something missing rather than just a problem with compiling. From my experience, the Java compiler can resolve such circular references if they are done correctly.


Java API Documentation
The Java Tutorial
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
This is the supporting class...it needs to have a String variable
Sting filename which it uses to do some analysis

public class analysis
{

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

JFileChooser fc;
File file;

String fileName="full1.txt";

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();

}
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
ohoh i forgot to add on that in the above code snippet...
in the analysis class String filename is also defined...that is to be overlooked as it has been already declared in the other class and the analysis class has to get it...my code wasnt runnin so i had added that piece...

Thanx
abhishek
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
Thanx guys...finally i got it right(through inner class)...though i m yet to figure what went wrong in the above way...
 
 
subject: Importing variables interdependent classes