|
Alright, apparently I don't understand how primitives and objects are passed to methods in Java. I thought Java only passes values to methods. But if I pass in an object like array, the object's content will change. For example:code:
code:
|
# ¿ Apr 25, 2008 00:19 |
|
|
# ¿ May 9, 2024 08:58 |
|
Twitchy posted:Arrays are themselves Objects, and so are passed by reference. The array may contain primitives (ints, doubles etc), but the structure that holds them is an Object. but if I change the scaleLine method to the following, the line array doesn't change... so if object parameters are really passed in as reference, shouldn't it point to the new int[] newLine? or is the following code just changing where the reference variable is pointing to and has no effect on the caller? code:
CrusaderSean fucked around with this message at 00:39 on Apr 25, 2008 |
# ¿ Apr 25, 2008 00:32 |
|
Twitchy posted:Hah, I think this same question was just asked on the last page, and being a master of words made a mess off explaining it, but basically: So in terms of programming practice, which of the following is better? void someMethod(Object obj...) { ... passed in object value may change ... } or Object someMethod(Object obj...) { ... explicitly return changed object ... } I see people using the first one because they want to change multiple objects in a method and can't return multiple objects. But in reality it is not really "void" in the sense that inputs can change in value so it might be misleading. Maybe this is a non-issue and everyone knows how reference works in Java... I was just really confused the first time I saw someone do that.
|
# ¿ Apr 25, 2008 00:51 |
|
I'm playing with java.util.Observer and Observable for MVC GUI design. I don't really understand how the listener/observers determine if the state has changed. I know you can run dataModel.hasChanged() to explicitly mark the data state as changed, but when is it necessary to do this? Does java automatically mark observable state as changed or do I have to do it manually? I redid this simple temperature program here. So when the temperature is raised or lowered, the GUI will call setTemperature function in the model. The model's setTemp function will in turn call the notifyObserver function. The temperature model does not call hasChanged() at all and it works just fine. So Java must have determined temperature state has changed somehow... Then I created my own example with a more complicated data structure. I'm using the same basic concept for observer and observable. I tried the following two variations: case 1: update state parameters, run notifyObserver => observer.update() doesn't run and nothing changes case 2: update state parameters AND set model.hasChanged(), run notifyObserver => will run observer.update() and GUI will reflect changes So does this mean I need to set model.hasChanged() manually for more complicated data models? CrusaderSean fucked around with this message at 03:25 on Apr 29, 2008 |
# ¿ Apr 29, 2008 03:22 |
|
triplekungfu posted:No, but is does call setChanged(), which means that any subsequent calls to hasChanged() will return true until clearChanged() is called. Hmm.. how did I miss that setChanged() line.. thanks for the clarification. edit: actually I was using setChanged(), I guess I'm illiterate that late in the night... CrusaderSean fucked around with this message at 16:11 on Apr 29, 2008 |
# ¿ Apr 29, 2008 14:48 |
|
I'm reading up on design patterns and I saw this interface declaration: public interface DrawingView extends ImageObserver, DrawingChangeListener {...stuff...} I thought java doesn't support multiple inheritance?... By the way, DrawingChangeListener is another interface. so interface extends another_interface doesn't really count as inheritance or what?
|
# ¿ May 2, 2008 19:54 |
|
|
# ¿ May 9, 2024 08:58 |
|
csammis posted:It doesn't for classes, but an interface can extend (and a class can implement) multiple base interfaces. Ah, k. That makes sense.
|
# ¿ May 2, 2008 19:57 |