• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Confused about using getter/setter for arrays

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did the code compiled successfully?
 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Arjen Dijksen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]); //???

 
Marshal
Posts: 76447
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 14306
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, C++ has const correctness:
 
Stephan van Hulst
Saloon Keeper
Posts: 14306
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually something that could have been awesome in Java:
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also create other setters/getters, such as:


or for a larger array,
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic