Results 1 to 14 of 14
Thread: Objects array placement
- 04-13-2011, 12:12 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
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
- 04-13-2011, 12:21 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
In Java there is no way to place anything outside of a class. The worst way (imho) is to do it this way:
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 SemiGlobals { public static Object[] 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[] getArray() { return array; } }
... 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.Java Code:public class AnyClass { private Object[] array; ... public Object getElement(int i) { return array[i]; } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-13-2011, 01:05 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
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!
-
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; } }
- 04-13-2011, 01:35 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
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?
- 04-13-2011, 01:42 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
-
.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.
- 04-13-2011, 03:43 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Strings are immutable, so you couldn't change it anyway.
If you had this:
The code in someTestCode() will result in the soc attribute of sc having its "a" set to 10.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; } }
-
Thanks for that Tolls. Your 'SomeClass' example passes the object around, which contains the field. What if you pass an objects field?
- 04-13-2011, 04:35 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
ozzy, I still don't know what the object of getObjectsWithAnAttribute is, please explain
- 04-13-2011, 04:39 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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:
and expect that new object to be reflected in someobject.Java Code:Something o = someobject.getSomething (); o = new Something ();
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.
- 04-13-2011, 05:02 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
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.
- 04-13-2011, 05:16 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
For all intents and purposes yes.
That's the purpose of making something private.
- 04-13-2011, 05:40 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
array of objects
By programcı in forum New To JavaReplies: 3Last Post: 04-06-2011, 02:28 PM -
rollover image with text placement
By DonK in forum New To JavaReplies: 1Last Post: 03-22-2011, 01:02 AM -
Random box game : unsuccessful Buttons & Graphics listneres and JPanel Placement.
By AcousticBruce in forum New To JavaReplies: 3Last Post: 01-12-2011, 10:43 PM -
need guidance in component placement
By rdtindsm in forum AWT / SwingReplies: 2Last Post: 05-10-2009, 04:14 PM -
Array of objects
By rosh72851 in forum New To JavaReplies: 5Last Post: 10-31-2008, 04:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks