This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing method in another file in another class  RSS feed

 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning Java on my own and have hit a snag.
This is such a basic question, but after searching my book, I cannot locate the answer.
Here goes:
I have 2 file folders in a directory.
Folder x has 5 classes in it, and folder y has 3 classes.
In folder x is a class that has a method that when coded, needs to call one of the methods in a class in folder y.
How do I access that method?
I know I have read this, but my brain has gone dead.
An example code would be:
doc = DataUtil.getDescription(fileName);
My error is "cannot find symbol".
Thanks for any help!
Eric
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are your different classes in different packages? If so, you will need to import the packages. Is that what you are looking for?
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they are just in seperate files.
The only thing I can think to do is to copy the class I need into the folder that holds the class I am calling it from.
Eric
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is copying this the best way to do this?
Thanks,
Eric
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the access modifier on these other classes?
You would normally have one class in each file (unless ClassA is specifically meant to be called only by ClassB) and give them public access. Does that get you anywhere?

CR
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The access modifier is public on each class.
Class DataUtility is in a file called ProductMaint
and Class DataTester is in a file called Tester.
I need to access a method in DataUtility from tne class DataTester in a file called Tester.
My question is:
These are not packages and the classes are all public and the method I need to access is a static method and each class is in a seperate directory file, what is the best way to access that method? Would it be the simplest and best just to copy it to the same directory my class that needs to access it is in?
Thanks,
Eric
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a diagram of the file set up:
ProductMaint
public class DataUtility
public static Document getDocument(filename)

Tester
public class DataTester
publilc static Document readFile()

From readFile() I need to call getDocument() in ProductMaint

Thanks again,
Eric
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried putting one class in one file? Did tha tmake any difference?
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to copy the class that has the method I need into the file that has the class I will be calling from.
I just thought there might be a better way to do it.
The simplest thing is to just copy it.
I appreciate your input.
Thanks!
Eric
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other ways to do this, by setting the class path (i.e. the CLASSPATH environmetn variable or the -classpath command option) to include more than one directory. See HowToSetTheClasspath for more info. However for the moment I think you should just go ahead and copy because that's easier, and because you're probably going to have some other problems here, so let's not do too many things at once.

In particular, you will really want to put each of your public classes in a separate file, with a name matching the class name (plus ".java"). Have you successfully compiled any files like this? What JDK are you using? I'd have expected the compiler to have complained before now. However even if you're able to successfully compile individual files with names that do not match the contents, you will likely have a lot more trouble gettting interdependent classes to recognize each other. Make the file names match the public classes contained inside, and you'll have a much easier time.
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book I am using has exercises at the end of each chapter and you download the info for the exercises.
In this exercise in one file directory there are 5 classes and in another file directory there are 3 classes. The class I need to access is in a directory different from the class I am accessing it from.
I know about putting classes in a file with the same name as the class, but they never do that.
I am using JDK 5.
To solve my problem, I copied the class I need to access into the directory of the class I am accessing it with. It compiled OK.
Any words of wisdom would be appreciated.
Thanks!
Eric
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really did everything they told you to do in the book, and it still didn't compile, have you asked the publishers?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunh. That's... interesting. It looks like in some of your posts you talk about "file folders" or "file directories" and in others you talk about "files" - but when you talk about files you never say what directories the files are in. I'm confused. Are ProductMaint and Tester files, or are they directories (folders)? If they are files, are they in the same directory, or different directories? I have a feeling we're using these words differently. What operating system are you using? And how are you editing your classes - with an IDE, or a text editor? Perhaps your environment looks very different than what I expect.
[ November 27, 2005: Message edited by: Jim Yingst ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In any event, I think Eric would benefit from reading a clear explanation of what Java packages are and how they're typically represented in the filesystem. Eric, have a look at the section of Sun's Java Tutorial that starts here, and see if that doesn't help you to understand some of the issues.
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using windows xp pro (sp2).
JDK 5.
My directory is like this:
My Documents\java5\chap20\
The dir on chap20 is:
ProductMaint
Tester
In ProductMaint there are 10 classes.
In Tester there are 3 classes.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to check out ant.
 
Eric Edwards
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, the class I need to access should be put into a package and after I do some more research, maybe into a jar file. This would be because it is a utility and would be used by other applications.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!