Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about using getter/setter for arrays  RSS feed

 
Arjen Dijksen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello dear coderanch community,

In my classes we have learned the importance of encapsulation. In this specific case it is about giving variables in (data)classes the acces modifier private. When making getters and setters for primitive private variables the stuff is very basic and easy to understand. However, I get a bit confused when it comes to arrays.

Example


Now I want to access (or change) the x and y from the object properly without making double xy[] public and using xy[0] = 1.0. How can I fill it using the setter?

thanks in advance,

Vib
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did the code compiled successfully?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Arjen Dijksen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to make more clear. Made some errors in first post.

Class Log


Main
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you have to do is use your getter to get a reference to the array, and then print from your reference.

Arjen Dijksen wrote:
//WANT TO PRINT l.xy[0] but this is not allowed because the array is private
//l.xy[0] works though if I make it public but my teacher will kill me
System.out.println(l.getXy[0]); //???
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can return a reference to the array, but there is a risk.Now your array in your object is totally messed up. Try it and see what happens. That is because you have an alias, which means two (or more) references to the same object. You will have to copy the array.Try it again and you find that you now have two separate arrays, and nothing can go wrong with the original version. The same applies when you are passing arrays to the constructor. Using clone() means I am taking a defensive copy. It would probably be better to use
Arrays.copyOf(xy, xy.length)
rather than clone().
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
It would probably be better to use
Arrays.copyOf(xy, xy.length)
rather than clone().


As internal/hidden workings in the setter also, so no stray references to the object will be laying around.

That has always been a problem with the way Java handles objects, even though, Java passes by value when you have an object you get a reference that is passed by value so you are, in actually, able to access the original object due to multiple references.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjen Dijksen wrote:


getXy is a method.
You're missing the () that Java expects from a method call.
getXy()[0].

But also take note of what Campbell says. It may not be entirely relevant to your immediate task, but it is a good habit.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:. . .
That has always been a problem with the way Java handles objects, . . .
Isn't that a standard problem for any OO language?
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, C++ has const correctness:
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually something that could have been awesome in Java:
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also create other setters/getters, such as:


or for a larger array,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!