Results 1 to 6 of 6
  1. #1
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default Access modifiers, why?

    Hi

    I've got a question about access modifiers.
    I know what each modifier does, but I can't find when you have to use wich modifier.
    Everyone says "use private, unless you can't", but why is that?

    Thanks in advance,
    Glenn

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by bubbless View Post
    Hi

    I've got a question about access modifiers.
    I know what each modifier does, but I can't find when you have to use wich modifier.
    Everyone says "use private, unless you can't", but why is that?

    Thanks in advance,
    Glenn
    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bubbless View Post
    I've got a question about access modifiers.
    I know what each modifier does, but I can't find when you have to use wich modifier.
    Everyone says "use private, unless you can't", but why is that?
    Because you don't want your privvy parts to be fondled with by arbitrary others.

    kind regards,

    Jos ;-)

  5. #5
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bubbless View Post
    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.
    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:

    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;
    }
    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 ...

    kind regards,

    Jos

Similar Threads

  1. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 06:20 PM
  2. Class Reflection: Finding class modifiers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:11 PM
  3. Access modifiers
    By bugger in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 10:32 AM
  4. Access with MS SQL
    By cachi in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 07:54 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •