Results 1 to 8 of 8
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default Reading arrays from different classes without getters and setters

    I have 2 arrays in different classes.

    Cards[100] is in CardArray.java and has 47 elements
    Map[501] is in MapArray.java and has 3 elements

    I've been reading a lot about trying to avoid using getters and setters. I want to edit either array while in the opposite class, but how do I do this?

    For example, when I'm in the CardArray class, how do I change the values of one of the elements in the Map array? Is there a way to do this without getters and setters?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    What do you mean by "avoid getters and setters"?
    There are several possible design styles that this could refer to...

    If it's a case of not exposing the array itself, then you simply provide add() and remove() methods (as in List, for example), and some form of iterator.

    If you're referring to the idea that getters and setters are indicative of something that isn't really Object Oriented, then you want to look at what you;re objects do, and not what they contain.

  3. #3
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    I just read a couple places that using getters and setters is bad in the long run. I don't know enough about java to understand what exactly they were saying.

    I'm writing what will end up being a very big program in the end and honestly am just stuck right now. I'm in CardArray.java and want to read and modify an array from a different class... Map.

    I have 2 arrays...

    Cards[100] which is 47 columns and 100 rows

    Map[501] which is 3 columns and 501 rows

    While I am in CardArray.java, I have built and populated my Cards[] array. Now I want to set the values of Map[101-200] element 3 to the value in Cards[1-100] element 47.

    How do I change the values of Map[] from inside a different class?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Psyclone View Post
    I just read a couple places that using getters and setters is bad in the long run. I don't know enough about java to understand what exactly they were saying.
    All generalizations are bad. :)

    Seriously though, judicious use of getters and setters is a good thing. Just don't expect that use of them will guarantee decent encapsulation. BTW, could you post links (or pseudo-links if you can't post links yet) to the articles stating that use of these is bad?

  5. #5
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Why getter and setter methods are evil - JavaWorld

    Typical Programmer - Doing it wrong: getters and setters

    A lot of the stuff they are talking about in these articles just goes over my head since I just started programming in Java a couple weeks ago.

    On a side note, I solved my problem by creating both arrays in the same class. In the end, I will probably have 10-50 arrays and am not sure whether it is good or not to have them all in the same class. Will this be a problem down the road? Right now the class is over 1000 lines long already.
    Last edited by Psyclone; 02-01-2010 at 06:23 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    While I think that the author's of those articles have some valid points, many don't agree 100% with them. Personally, I fear that avoiding all getters/setters to avoid overuse is like throwing out the baby with the bath water.

    In the end, I will probably have 10-50 arrays and am not sure whether it is good or not to have them all in the same class. Will this be a problem down the road?
    Without seeing your program, or knowing the code structure, it's hard to say, but it smells to me like it could be trouble.

    Right now the class is over 1000 lines long already.
    This sounds like a classic "god-class". You should seriously consider refactoring that class without too much delay.

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    What's a getter and a setter? Is it a getSomething() method and a setSomething() method?
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    The idea in those articles is fine, however you do have to understand OO like the back of you hand in order to develop like that. And I really don't think you're there.

    Oh, and it won't work with a lot of frameworks out there that rely on get and set methods.

Similar Threads

  1. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 05:55 PM
  2. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  3. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 12:57 AM
  4. Replies: 1
    Last Post: 02-19-2009, 03:50 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 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
  •