aspose file tools*
The moose likes Beginning Java and the fly likes question about what makes a class public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about what makes a class public" Watch "question about what makes a class public" New topic
Author

question about what makes a class public

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11


So I need to create a public class for this project I'm working on for school.

My question is, aren't ALL classes public unless declared private (like instance variables)?

Another facet to this question is.... can I have private instance variables in a public class?

Thanks a lot,
Janeice


When you do things right, people won't be sure you've done anything at all.
Ruben Guillen
Greenhorn

Joined: Sep 02, 2009
Posts: 28
Dear Janeice

1) if we are talking about non-inner classes, normally classes could have only two possibles access modifiers: public and none (which indicates as default access).

There is a restriction of the compiler that only one public class could be defined per physical file with the extension java and the name of the file should be equal to the name of the public class, if there is not any public class inside the file then this restriction does not apply.

2) instance variables (no local variables) could have one of the following access modifier public, protected, none (default), private.

Regards.


SCJP, OCMJD
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
My question is, aren't ALL classes public unless declared private (like instance variables)?


Nope, all outer classes are set to the default access unless declared public. Having a private outer class isn't meaningful, since you could never reach it. Inner classes can be private (or otherwise).

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

So..... (I deleted all the functional code here and left the declarations)



..... and everything will continue to work as is? REALLY??? Ummm..... the answer seems too simple here. Do I change anything in the way I refer to any of these methods or instance variables?

Thinking this should be harder,
Janeice
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

You probably want to make your member variables private (right now they're at the default access level), and your methods public (if that makes sense). Declaring a class public only affects that class, and not the methods or member variables.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11


John de Michele wrote:Janeice:

You probably want to make your member variables private (right now they're at the default access level), and your methods public (if that makes sense). Declaring a class public only affects that class, and not the methods or member variables.

John.

John, thanks
So it needs to look like this?



.... and NOTHING needs to be done to the other existing classes?


--Janeice
Ruben Guillen
Greenhorn

Joined: Sep 02, 2009
Posts: 28
Dear Janeice

Please give a look here for information on access modifiers.

Regards.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

You need to remove the access modifiers from the arguments to your methods, since that doesn't make sense. Other than that, you should be good.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

John de Michele wrote:Janeice:

You need to remove the access modifiers from the arguments to your methods, since that doesn't make sense. Other than that, you should be good.

John.


Hehehe.... when you said "member variables" I thought you meant the argument variables. I didn't have my instance variables listed and you said my "member variables... [were] at default access." Inferring that got me in trouble.

Thanks.
Janeice

P.S. Thanks for the link Reuben!
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

Sorry about that. I was actually referring to your original statement that member variables are automatically set to private. Arguments are the variables that go into method signatures. Member variables are instance variables.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

When I said:

My question is, aren't ALL classes public unless declared private (like instance variables)?


I meant...

"Instance variables are public unless declared private. I haven't been declaring my classes 'private,' so that leads me to believe my classes are ALREADY public."

I am so glad we cleared this up.
Janeice
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

LOL

John.
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
Janeice DelVecchio wrote:
I meant...

"Instance variables are public unless declared private. I haven't been declaring my classes 'private,' so that leads me to believe my classes
are ALREADY public."


Class members declared with no access modifier are package-private. This is different than public in that such members could be
accessed by other classes in the same package, but not by other classes in other packages.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

I haven't started using packages yet... I figured they were public when I could use any main method with any of the classes I made......

Bad assumption....

Janeice
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: question about what makes a class public