• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Friendly Access

 
Manickam Periyanan
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not mentioned any access specifier .So it is friendly.
 
Barry Higgins
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes It looks like it.

 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic