Hi, I have defined 2 class in a file .One is public class with the same as the file name.other one is a helper class. I tried using the helper class in new class(Tried to create it using new helperclass()) but it is giving error saying class doesn't exist. All of them are in same package.since the helper class is friendly it should be available with in the package to all classes.why does not it allow me.Can somebody tell me why.
Please change your display name to match the JavaRanch naming policy, that is, your 1st and last name.
As for your question...what access level did you make the helperclass (public, private, protected). Make sure that you have the constructor for the helperclass in place. The code for creating a new instance of it should be
If you have a default constructor in helperclass with the form
Hope this helped some...
Joined: Nov 22, 2004
I have not mentioned any access specifier .So it is friendly.
When you have two classes in the same file, the "helper" class can only be instantiated in that file. If you want to create an instance of it in another class outside of that file, the "helper" class needs to be in its own file all by itself. You can still leave it as default access.
I'd like to point out that "friendly" access isn't the correct terminology. There may be texts that use this, but the official Sun texts I have seen use "default" access to indicate when no access modifier is given.
p.s. In the future, you should post the exact error message. (Copy and paste is your friend.) Summarizing typically loses crucial information. [ December 08, 2004: Message edited by: Layne Lund ]
Originally posted by Layne Lund: When you have two classes in the same file, the "helper" class can only be instantiated in that file. If you want to create an instance of it in another class outside of that file, the "helper" class needs to be in its own file all by itself. You can still leave it as default access.
This is not true. All other non-nested classes declared in the same compilation unit will be compiled as top-level classes and can be accessed by *any* class in the same package. You just have to explicitly compile them before the classes that refer to them. Putting them in files all by themselves only eliminates the need to explicitly compile them before any classes that reference them.
Junilu, I have updated my profile. But as far as the reply you have sent I don't understand what you mean by explicity compiling them.Could you please try something similar and let me know what could be problem .Since all the classes are in the same directory / package.I can see the class .And if access in the class in the same file then there is no problem.But if I access from some other file /class but It says cannot resolve symbol..... Has anybody come across this problem.. THnks
The Java compiler has a kind of built-in "make" feature that eliminates the need to manually keep track of class dependencies and worry about the order of compilation. Before compiling a class, the compiler will scan the classpath for any referenced classes first (In your case, Two references Helper). It will try to find .class files with the same name, then it will try to find .java files with the same name. If a .java file is found, it's timestamp is compared with the .class timestamp. If the .class file is older, the .java file is compiled. If the .java file is not present, the Java compiler takes the first available .class file. If it can't find a class file, it will complain with a "class not found" compilation error.
When you have more than one class in a source file (formally called a "compilation unit"), however, there is no way for the non-public files to participate in the automatic dependency tracking described above. So, you have to explicitly compile One.java before Two.java so that the compiler can see the Helper.class file when it compiles Two.java.
Make sure there is a Helper.class file before you compile the other class.