permaculture playing cards*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifiers (very simple Q)" Watch "Access modifiers (very simple Q)" New topic
Author

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?

Thanks.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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.


SCJA
~Currently preparing for SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
. . . 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
Greenhorn

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 http://pythonicway.blogspot.com/
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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.



My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Access modifiers (very simple Q)
 
Similar Threads
Can one object access a private variable of another object of the same class?
Variable Scope
subclasses and access modifiers
inner class & this statement in constructor
how to add more methods to class methods