Results 1 to 5 of 5
  1. #1
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default Quick Question...

    Just a quick question from a newbie. Generally speaking, does encapsulating your data add length to the source files versus not using encapsulation?

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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Well, your source files will be longer since you have to provide getters and setters, which would add 3 lines each. But that should really not be a concern at all.

    Objects of this class would be the same size in memory.

  4. #4
    FatalSylence is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Well, your source files will be longer since you have to provide getters and setters, which would add 3 lines each. But that should really not be a concern at all.

    Objects of this class would be the same size in memory.
    This is what I meant.

    Thank you for your reply. I just want to be very cautious about doing things right.

    Do you necessarily have to have a getter AND a setter? For instance, I have JUST a getter and it works fine for my purposes.
    Java Code:
    public int getStar1() {
        return firstStar;
    }
    I take the getStar1() method in another class and perform operations on it.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    You don't need both.

    If there is no reason for something external to aninstance of a class to set something then a setter should not exist. And vice versa. If you have no need to get an attribute from a instance of a class then there is no need for a getter.

    For example you might see a lot of Spring (it's a framework) stuff where the Java files only have setters.

Similar Threads

  1. Really quick question
    By shadycharacter in forum New To Java
    Replies: 2
    Last Post: 04-22-2010, 10:06 PM
  2. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 04:13 AM
  3. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  4. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM

Posting Permissions

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