Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2010, 03:39 PM
Member
 
Join Date: Jan 2010
Posts: 78
Rep Power: 0
Psyclone is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-01-2010, 04:31 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-01-2010, 04:45 PM
Member
 
Join Date: Jan 2010
Posts: 78
Rep Power: 0
Psyclone is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-01-2010, 07:05 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,483
Rep Power: 8
Fubarable is on a distinguished road
Default
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?
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-01-2010, 07:18 PM
Member
 
Join Date: Jan 2010
Posts: 78
Rep Power: 0
Psyclone is on a distinguished road
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 07:23 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-01-2010, 07:36 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,483
Rep Power: 8
Fubarable is on a distinguished road
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.

Quote:
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.

Quote:
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-01-2010, 11:21 PM
Lil_Aziz1's Avatar
Senior Member
 
Join Date: Dec 2009
Posts: 146
Rep Power: 0
Lil_Aziz1 is on a distinguished road
Default
What's a getter and a setter? Is it a getSomething() method and a setSomething() method?
__________________
"Life is not the amount of breaths you take, it's the moments that take your breath away.." (Hitch)
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-02-2010, 12:01 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Positions and values, getters, setters Malus New To Java 10 01-23-2010 06:55 PM
Getters and Setters lheviathan New To Java 4 11-02-2009 02:47 AM
methods, classes, arrays.. oh my! katalyst New To Java 30 03-30-2009 01:57 AM
[SOLVED] Problem with program(getters/setters,arrayList). Pierced1 New To Java 1 02-19-2009 04:50 AM
Getters and Setters Charliestons New To Java 10 09-12-2008 11:57 PM


All times are GMT +2. The time now is 01:27 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org