Results 1 to 6 of 6
  1. #1
    lenah is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default java-Encapsulation

    AS far as encapsulation is concerned, private variables are declared to have set/get methods. but what about boolean variable.

    if i declare a boolean variable as private,
    it takes isVariableName?

    can any one comment on this?

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

    Default

    The type of the variable is doesn't matter at all. You can use get/set method or any other custom name for a method.

  3. #3
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    It's a matter of preference, I personally would use getIsVariableName/setIsVariableName

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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by racerxadam View Post
    It's a matter of preference, I personally would use getIsVariableName/setIsVariableName
    By far the most common convention is to use set/is for booleans and set/get for all other types.

    getIs/setIs appears horribly contrived.

    db

    edit

    In addition, this is part of the Java Beans specification. Click the link on
    API Specifications for JavaBeans
    (Requires login to the Sun Java Developers Community)

    Section 8.3.2 says:

    8.3.2 Boolean properties

    In addition, for boolean properties, we allow a getter method to match the pattern:
    public boolean is<PropertyName>();

    This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method,
    or it may be provided in addition to a “get<PropertyName>” method.

    In either case, if the “is<PropertyName>” method is present for a boolean property then we will
    use the “is<PropertyName>” method to read the property value.

    An example boolean property might be:
    public boolean isMarsupial();
    public void setMarsupial(boolean m);

    Or see the JavaBean Example on
    JavaBean - Wikipedia, the free encyclopedia
    Last edited by DarrylBurke; 01-05-2009 at 01:40 PM. Reason: Added information

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Darryl is correct in what he says. The big reason, which Darryl alludes to, is that you want to follow the Java Beans conventions. That enables many tools and even some built-in functionality (XML serialization) to work.

Posting Permissions

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