Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Access modifiers (very simple Q) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifiers (very simple Q)" Watch "Access modifiers (very simple Q)" New topic

Access modifiers (very simple Q)

Ravaa Bal
Ranch Hand

Joined: Apr 15, 2009
Posts: 31
I'm puzzled about the use of public/private/protected/ etc. From what I've read I understand what they essentially do, but I don't see why I should set something to anything other than public if it isn't necessary. Or is it? I'm guessing it has to do with resources, something or other. Is there a very good reason why setting everything to public is a bad idea?

And if it is necessary, should I set every class to private until I find that it needs public access?

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

And if it is necessary, should I set every class to private until I find that it needs public access?

At the actual class level, such as public class HelloWorld {...} , you'll be safer defaulting to public. If it's private, even the public main methods won't be available to the Java runtime, which will cause you endless headaches as you wonder why the simplest pieces of code don't want to work.

So, what can be public? Classes, methods and instance variables can have access modifiers placed upon them.

For the most part, the vast majority of classes you create will be public.

The vast majority of instance variables you declare will be private. Data is important, and you don't want everyone to have direct, unimpeded access to your data.

Methods that work internally on data, and aren't used by clients, but are really part of the internal working of the class should be private. If other components need to call a particular method, you need to make it public.

It's always a balancing act, that's for sure. Initially, I think seeing four access levels seems overwhelming, but the more you program, the more you'll see cases where the use of protected or default access is not only smart and clever, but it's also elegant. I remember seeing the Factory pattern implemented propertly for the first time, and I thought "wow, that really makes sense."

Plus, a Java class file can only have one public class in it. I sometimes like to add comparator classes to a Java file where only classes in that Java file really needs to call the class implementing the comparator interface. Since only one class in a single Java file can be public, these second and third classes must have either a package or protected access modifier.

It'll grow on you. Give it a chance.

-Cameron McKenzie
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Always make anything you declare as restrictive as possible. If everything were public than your objects would have no control over thier own properties. Take for example a simple class Person which has a private int numberOfArms. Normally another object could change this variable by passing a value to the setArms(int num) method. This method is public, but it makes a check inside to make sure you don't give someone too many arms. If numberOfArms was public then another object could just randomly assign a Person with 234242 arms. As a rule your objects should make everything private and provide access modifiers (getters, setters) to access and change them. Protected and default access I don't use much except when setting up inheritance.

Hope that helped somewhat.

~Currently preparing for SCJP6
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
. . . as Brian Legg has said, use "get" and "set" methods.

Should you need to use "set" methods to change anything, you can validate the changes in the "set" method, and only implement them if you are happy with them.
karan khosla

Joined: Apr 27, 2009
Posts: 25

public ---> Accessed from Anywhere
protected ----> Accessed Within the Same Package and Outside the Package via Inheritance Only.
default (a.k.a Package private) --------> Accessed Within the Same Package only.
private -------> Cannot be Accessed outside the class.

For Access Modifiers one diagram should be in Mind

I Hope the Information is Helpful to you.

Karan Check me out on
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1111

From approach point of view.....

Suppose you made a class emulating a Stack, would you want topOfStack to be editable by all ?
exposing topOfStock would mean opening up the program's internals for errornous coding.

Though this is not such a trivial issue, issues like this lead to poor programming, hence the use
of access modifiers help us to write better programs.

I agree. Here's the better link:

I agree. Here's the link:
subject: Access modifiers (very simple Q)
It's not a secret anymore!