• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

array in a java class

i read like wrapping array in class protects array from being inadvertently altered. i have not understood what it means. Can you please advise with some sample code, link, resource.
  • 2
3 Solutions
Dung TrinhMarching Band Field CrewCommented:
Do you mean setting it as a private variable?
Dung TrinhMarching Band Field CrewCommented:
I think this is what that sentence mean

public class Myclass 
      private int[] myArray = new int[60];

Open in new window

Now no one can mess with your array. Only the function in Myclass can change the data in myArray
zzynxSoftware engineerCommented:
I think Dung is right.
If you do it like that, everyone using your Myclass can't change the internal/private int array. They don't even know it exists.
The only thing they can do is calling the public methods of Myclass to manipulate the data in it.
So as the developer of Myclass you (and only you) decide how you fill up and read from that array and nobody from "outside" can tamper with the data inside the Myclass.
Also, if later on you decide to replace the array with something else (e.g. a list), nobody "outside" should know or even care about that, as long as you keep the same methods (e.g. to add and get data values) your class keeps working as a user expects it to work.
Tomas Helgi JohannssonCommented:

You can define the array variable as public, private or protected depending on which access level method you would like it to have in your coding design.
Look at these

    Tomas Helgi
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now