File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Friendly Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Friendly Access" Watch "Friendly Access" New topic
Author

Friendly Access

Manickam Periyanan
Greenhorn

Joined: Nov 22, 2004
Posts: 4
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.

THnks
ManickamPR.
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
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...
Manickam Periyanan
Greenhorn

Joined: Nov 22, 2004
Posts: 4
I have not mentioned any access specifier .So it is friendly.
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
Can you show us the code you have presently. From what you are describing it should look something like this



which will work fine
Manickam Periyanan
Greenhorn

Joined: Nov 22, 2004
Posts: 4
yes It looks like it.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

HTH

Layne

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 ]

Java API Documentation
The Java Tutorial
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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 - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

"COOOL BASHA"

Please change your profile so that your publicly displayed name complies with the JavaRanch Naming Policy. Thanks for your cooperation.
Manickam Periyanan
Greenhorn

Joined: Nov 22, 2004
Posts: 4
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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5



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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Friendly Access
 
Similar Threads
helper functions good design?
NX: URLyBird: how to start with data access
Can't load non-EJB class?
java import packages question
criteriaFind().. implementation