[java] simple question

Started by
5 comments, last by DanLatimer 18 years, 11 months ago
What happens if you declare a class private in the global namespace? lets say I have this:

public class someclass
{
public static void main(String[] args) {System.out.println("test");}
};

But what happens if the class is private? Do I just not have access to it or what?
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Advertisement
Only inner classes can be private and then can obviously only be instantiated by their enclosing class. It wouldn't make much sense to make an outer class private :)

D.
A .java source file must contain exactly one public class at top level. Other classes may be declared as just "class" (no ("public")) within the source file; such classes (the entire contents; i.e. your ability to create objects of the class, refer to the object type, or use any statics) are accessible (IIRC) only within that source file.

I probably have some details wrong. It shouldn't be too hard to research this via java.sun.com.
Quote:Original post by Zahlman
A .java source file must contain exactly one public class at top level. Other classes may be declared as just "class" (no ("public")) within the source file;


this is correct.

Quote:Original post by Zahlmansuch classes (the entire contents; i.e. your ability to create objects of the class, refer to the object type, or use any statics) are accessible (IIRC) only within that source file.


other top level classes within the same java file actually have package scope. If you want a private class available only to the top level class in the java file you have to declare it within the top level class and declare it private. If its declared within the enclosing class and has public or package level access then you can still instantiate it using the following rather unintuitive syntax:

where, Enclosing is the outer class type, enclosing is an instance of the enclosing class, Inner is the enclosed class type ...

Enclosing enclosing = new Enclosing();
Enclosing.Inner innerclass;
innerclass = enclosing.new Inner();

depending on the access restrictions IE if you declared it public you could
do that from anywhere, if it has package level access you can only do it from
the same package.

-whew- !!

D.









Quote:Original post by daireq
other top level classes within the same java file actually have package scope.


Yes, I was supposed to know that :)

Quote:Enclosing enclosing = new Enclosing();
Enclosing.Inner innerclass;
innerclass = enclosing.new Inner();


Indeed. This is because in Java, instances of inner classes carry an implicit reference to an enclosing instance. There is a bunch of odd trickery with these things, which boils down to them giving you a lot more than you usually need (disastrous for J2ME).
ok,thanks.
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
your coding isnt properly lined! im furious :@! hit the enter key next time! ;)

This topic is closed to new replies.

Advertisement