Jump to content
  • Advertisement
Sign in to follow this  
supercoder74

[java] simple question

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.









Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!