3 Replies Latest reply: Aug 4, 2008 8:33 PM by 807589 RSS

    private modifier

    807589
      I have a class with a field (property) array of type integer
      I want this field to be unique for every instance of the class
      And not shared by other instances
      1-     I already declare it as private
      2-     And when access it (adding or deleting) I use �this� key word
      But still the different objects created from this class are accessing the same array I want every object to have its own array, any suggestions?

      Edited by: mayoya on Aug 4, 2008 6:00 PM
        • 1. Re: private modifier
          807589
          If you don't call it static, every object will have its own array. Unless, of course, you are constructing them all by passing them the same array.
          • 2. Re: private modifier
            807589
            Ottobonn wrote:
            If you don't call it static, every object will have its own array. Unless, of course, you are constructing them all by passing them the same array.
            Every object will have its own variable, but not necessarily the same array. In fact it's quite conceivable (and likely) that all the variables refer to the same array object. To the OP: Do you provide each variable with a copy of the original array?
            • 3. Re: private modifier
              807589
              every object will have its own array. Unless, of course, you are constructing them all by passing them the same array.
              Each instance will get its own reference to an array. But, as Ottobonn says, it's up to you to make sure the references refer to different arrays. Check that for each instance "new int[]" is being used somewhere - most likely in the constructor or, if your property has a setter method, by whoever calls that setter.