File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes understanding access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "understanding access modifiers" Watch "understanding access modifiers" New topic
Author

understanding access modifiers

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i have this thread:
http://www.coderanch.com/t/395617/java/java/first-interface
where access modifiers publicand private appear.
I understand the abstract meaning of each one; but what i wanna get is when and why we use them, actually in the code in the page i refer to.
In other words: does one need to say public/private because its necessary or its just a question of elegance?
(remember i mean in my code)
thanks in advance


java amateur
Jason Kingsley
Greenhorn

Joined: Feb 21, 2004
Posts: 18
In answer...
The use of access modifiers is never a question of elegance, but of correctness.
Although, it's fair to say though that if you are writing a small program that is for your very own personal consumption that you might just go "What the heck" but that would be actually quite tardy and bad form and a programmer should always be aware of the implications of the code they are writing.
When writing your classes which define the set of objects you will be using in your programs you need to give thought as to who will be using your objects and how will they be using them.
Accessors are there to provide access security to your objects. Your objects naturally define data and methods that work on that data and allowing anyone (or object) unfettered access is going to cause bugs.
For example, you may define a class that other people may use in their code. If you make a method public (lets call it foo() for now) then you have set up an implicit contract with the consumer of your class. You have exposed behaviour that generally you must then continue to provide. Further if the method is not final then consumers of your class might be able to modify that behaviour somewhat in their subclasses by redefining foo() - and if you as the original base class designer were relying on your implementation of foo() to do something in a certain way, or modify state in a certain way then you are now at the mercy of the client programmer who has redefined your method as Java is late binding and you'll call through into their subclass.foo() implementation.
Generally lock down access to your object as much as possible. Its easier to provide a feature later to a consumer (or yourself) then to take it back again because "once public, forever public" is a common mantra in the commercial world...
[ February 21, 2004: Message edited by: Jason Kingsley ]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i did understood now
thanks a lot
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: understanding access modifiers