Results 1 to 6 of 6
Thread: Access modifiers, why?
- 11-01-2009, 06:18 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
- 11-01-2009, 10:12 PM #2
usually you would want to use private fields to hide the implementation details not relevant to something external. The idea, is the object is to perform a certain function, and the contract (our coupling) with the external 'world' should be done through methods to manipulate the state of the object.
see also: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 11-02-2009, 01:55 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, travishein gives you a better explanation. As he said the whole idea is hiding unnecessary details about your implementation to the outside. The following article may also helpful to you.
Introduction to Java Access Modifiers | Java Beginner
- 11-02-2009, 08:04 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
- 11-02-2009, 12:41 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
And what if you are the only programmer of the project?
Is it necessary to make them all as restricted as possible?
And why would you make variables/methods that other programmers can't access?
I know the basics about it and who can access what but I'm trying to understand when to choose certain modifiers.
- 11-02-2009, 01:40 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
Even if you're the only programmer on the project: don't even trust yourself, you also make mistakes, no matter how small or how innocent; it's a good thing that your software protects you against your own mistakes.
A small example: suppose an int property needs to have an even value for the object to function properly. It is way better to make it a private property and implement a small setter method like this:
Suppose the property member variable would've been public; everyone, including yourself can do things like: myObject.propery= 41; and things keep on working for a little while and all of a sudden: crash, disaster and no stack trace indicating who or what did it ...Java Code:private int property= 0; // must be even public int setProperty(int property) { if (property&1 == 1) throw IllegalArgumentException("property must be even: "+property)l this.property= property; }
kind regards,
Jos
Similar Threads
-
Method access or field access
By carderne in forum New To JavaReplies: 2Last Post: 12-06-2008, 06:20 PM -
Class Reflection: Finding class modifiers
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:11 PM -
Access modifiers
By bugger in forum New To JavaReplies: 5Last Post: 11-28-2007, 10:32 AM -
Access with MS SQL
By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 07:54 AM -
Help with access a database using Microsoft Access
By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 07:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks