Results 1 to 14 of 14
  1. #1
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default Objects array placement

    Is it best to have an object array in its own class as a private attribute or best to have outside the class if it's needed by other classes?

    I like to leave them inside the class then have a getObjects method but then I need to create an object of that class to store the array into.

    Is there a better way?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by garrym View Post
    Is it best to have an object array in its own class as a private attribute or best to have outside the class if it's needed by other classes?

    I like to leave them inside the class then have a getObjects method but then I need to create an object of that class to store the array into.
    In Java there is no way to place anything outside of a class. The worst way (imho) is to do it this way:

    Java Code:
    public class SemiGlobals {
       public static Object[] array;
    }
    That way your array can always be reached (SemiGlobals.array), even by parties that don't need to deal with the array; Just doing this:

    Java Code:
    public class AnyClass {
       private Object[] array;
       ...
       public Object[] getArray() { return array; }
    }
    ... is just a bit better: there is no need to implement a setter for the entire array but it still can be ruined by anyone. Still a bit better (imho) is:

    Java Code:
    public class AnyClass {
       private Object[] array;
       ...
       public Object getElement(int i) { return array[i]; }
    }
    ... it is nobody's business that the objects are stored in an array; the int index value is just a bit of a giveaway. You can make the AnyClass a singleton if you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    Jos, by doing it the last method you described are you suggesting that externally you can only access individual objects stored in the singleton object? Therefore any calculations requiring the full object array would be dealt with only by the singleton object and not outside? Thanks!

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I like to keep all search methods centralised into the class itself, so that instead of having loads of loops dotted around in your other classes, you can call one method to loop from the class which holds the array. Something like this:

    Java Code:
    public class AnyClass {
        private Object[] array;
        ...
        public List<Object> getObjectsWithAnAttribute(ObjectAttribute tada) {
            List<Object> myList = new ArrayList<>();
            for (Object o:array) {
                if (o.objectAttribute.equals(tada)) myList.add(o);
            }
            return myList;
        }
    }

  5. #5
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    ozzyman, don't all the objects in the class have the same attributes?

    I'm a bit fuzzy when it comes to by reference and by value. If you're calling a method that returns an attribute of an object array that's by ref, right? What you're doing is creating a new object array in the method so it doesn't affect the object attribute. Is the attribute argument there just for added security in case the programmer's mistaken the object type?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by garrym View Post
    Jos, by doing it the last method you described are you suggesting that externally you can only access individual objects stored in the singleton object? Therefore any calculations requiring the full object array would be dealt with only by the singleton object and not outside? Thanks!
    Yep, but I don't know your requirements; I always use this rule of thumb: only expose as much of your privvy parts as necessary but no more. That exposusre can/must be done by calling methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by garrym View Post
    ozzyman, don't all the objects in the class have the same attributes?

    I'm a bit fuzzy when it comes to by reference and by value. If you're calling a method that returns an attribute of an object array that's by ref, right? What you're doing is creating a new object array in the method so it doesn't affect the object attribute. Is the attribute argument there just for added security in case the programmer's mistaken the object type?
    .equals checks by value
    == checks by reference

    if you call the method with the parameters as references then == will return true, but if you call the method with the parameters as values i.e. searching for a customers name by customer input form, == will return false so you use myString.equals(otherString) as your condition instead.

    and i'm not sure about this but.......
    if you had the get method: public String getName (Object obj) {return obj.name};
    and fed it into a String; String hisName = getName();
    editing the string 'hisName' shouldn't edit the original object?

    Hopefully someone who knows for sure will clarify...
    Last edited by ozzyman; 04-13-2011 at 03:15 PM.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,819
    Rep Power
    19

    Default

    Quote Originally Posted by ozzyman View Post
    and i'm not sure about this but.......
    if you had the get method: public String getName (Object obj) {return obj.name};
    and fed it into a String; String hisName = getName();
    editing the string 'hisName' shouldn't edit the original object?

    Hopefully someone who knows for sure will clarify...
    Strings are immutable, so you couldn't change it anyway.

    If you had this:
    Java Code:
    class SomeClass {
        private SomeOtherClass soc = new SomeOtherClass();
        public SomeOtherClass getSoc() {
            return soc;
        }
    }
    class SomeOtherClass {
        public int a = 0;
    }
    class MyTest {
        private SomeClass sc = new SomeClass();
        public void someTestCode() {
            SomeOtherClass soc = sc.getSoc();
            soc.a = 10;
        }
    }
    The code in someTestCode() will result in the soc attribute of sc having its "a" set to 10.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Thanks for that Tolls. Your 'SomeClass' example passes the object around, which contains the field. What if you pass an objects field?

  10. #10
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    ozzy, I still don't know what the object of getObjectsWithAnAttribute is, please explain

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,819
    Rep Power
    19

    Default

    Quote Originally Posted by ozzyman View Post
    Thanks for that Tolls. Your 'SomeClass' example passes the object around, which contains the field. What if you pass an objects field?
    soc is a field. As much as "a" is in SomeOtherClass.

    Just remember that Java is pass by value. Primitives (ie attribute a in SomeOtherClass) are therefore passed by their value, which means any change to that value is not reflected back.

    Objects are passed by value as well, so you can't do:
    Java Code:
    Something o = someobject.getSomething ();
    o = new Something ();
    and expect that new object to be reflected in someobject.
    But if o had a setter then I could change the data within o which would be reflected in the object stored in someobject.

    The thing with your String example is that it has no such stter. It is immutable.

  12. #12
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    So if you want to change a value of a private attribute, the only way you can do it is through a setter in that class, if you're accessing the class object from outside?
    Last edited by garrym; 04-13-2011 at 05:05 PM.

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,819
    Rep Power
    19

    Default

    For all intents and purposes yes.
    That's the purpose of making something private.

  14. #14
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

Similar Threads

  1. array of objects
    By programcı in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 02:28 PM
  2. rollover image with text placement
    By DonK in forum New To Java
    Replies: 1
    Last Post: 03-22-2011, 01:02 AM
  3. Replies: 3
    Last Post: 01-12-2011, 10:43 PM
  4. need guidance in component placement
    By rdtindsm in forum AWT / Swing
    Replies: 2
    Last Post: 05-10-2009, 04:14 PM
  5. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •