• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access modifiers (very simple Q)

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . 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.
 
Greenhorn
Posts: 25
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic