*
The moose likes Beginning Java and the fly likes Passing same objects in a class constructor and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing same objects in a class constructor and methods" Watch "Passing same objects in a class constructor and methods" New topic
Author

Passing same objects in a class constructor and methods

Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
Hi all,

Just wanted to say really great site..just a quick (and possibly dumb) question. Take the following code snippet;

public class MyClass
{
private SomeObject o;
private AnotherObject a;
private List list;

public MyClass(SomeObject o, AnotherObject a)
{
this.o = o;
this.a = a;
}

public void addSomething(SomeObject o)
{
list.add(o);
}

public void addAnotherThing(AnotherObject a)
{
list.add(a)
}

You will notice that i'm passing in the same objects in the 2 methods as the ones that are passed in my constructor. Does this make sense to do this? Are the Objects passed in my constructor the same as the ones passed in my methods?

Thanks in advance,


Jay
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Yes and no. No,simply because list is null.

You are adding more objects to the list, so it would make sense. However, if you are passing the same objects to the methods that you passed in the constructor it would not make sense. Just because the arguments have the same name does not mean they are the same objects.

The only thing that can be inferred by your code is that the arguments of the methods are the same type as the arguments in the constructor.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Constructor is also one type of method for which no return type is there.
I don't think there is any difference ,whether you pass some object to constructor or to the method.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Constructors are not a type of method, they are not a method at all. They have one purpose(to create an instance of the class), return nothing, are never members, so can not be overridden. Other then the arguments they have no real similarities.
Nalaka Gamage
Greenhorn

Joined: Nov 15, 2006
Posts: 8
yes its same. Constructor is when you initiate objects. Methods can be called any time.

Regards,
Nalaka
http://javatouch.googlepages.com
Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
The only thing that can be inferred by your code is that the arguments of the methods are the same type as the arguments in the constructor.


Dave, so are you saying that it doesn't make sense to have the same TYPE within the constructor and it's methods?

Thanks,

Jay
Rachil Chandran
Ranch Hand

Joined: Mar 05, 2006
Posts: 67
Having the same type passed to the constructor and setter methods does make sense.

What Dave meant was having the exact same object (not just type ) passed to both the methos and constructor does not make sense.

AnotherObject ao = new AnotherObject();

ao.setValue = 1; //lets say this sets a member of this object.

MyObj m = new MyObj(ao);//passing ao to constructor
m.addObj(ao); //again passing the same object to the method

Here the constructor and addObj method of MyObj take the same type of argument (AnotherObject). This is alright. But in the above scenario we are passing the exact same object to both. This is probably not required (can't think of any scenarion where this may be useful).


SCJP2 96%
Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
But in the above scenario we are passing the exact same object to both.


Actually, even though i am passing the same object type and name in my method and data members, it doesn't mean that it's the same object. I think (Shadowing)?

I just don't understand how this makes sense. Why would you want to add more than of the same object type in a method within a class who's constructor already gets it passed in?

Thanks,

Jay
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
In your example you are adding objects of the same type to a list. The names of the references in the arguments are really just placeholders,the object might be significant outside the method, but not the name of the reference.

Maybe this will be illustrative:





As you see it makes perfect sense to do this. This is a contrived and silly example but hopefully it helps make things clear. Since Example holds a list of unique identifiers, no two String objects will have the same value. You could change it to hold names, and in that context it would make sense to pass objects of the same value.

Or a simpler example:



Here we have 1 type(Integer) and three distinct objects. Even though two have the same name and all three the same value, they are 3 separate objects. val, val1 and val2 all reference different objects.

Look over the difference between type and object of that type and check out variable scope.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Maybe this is a better example. This is a very simple mutable integer wrapper class.

Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
Thanks Dave for the examples..I think the last one cleared things up...

so for your last code example:

public class MyInteger
{
private int val;

public MyInteger(int val)
{
this.val=val;
}
public void setValue(int val)
{
this.val=val;
}
public int getValue()
{
return val;
} ...


Will the getter be returning whatever value "val" is that was passed into the method?
Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
whoops..sorry Dave, i meant to say, will the value of "val" be whatever i passed into the SETTER method, and not the value of what was passed into the constructor (because they are in essence, not the same value).
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
This is code elsewhere instantiating and manipulating MyInteger


put the main method in MyInteger, or another class, and play around with it, creating multiple objects of MyInteger and play around with it, printing out values and see if what is returned is what you expect.
Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
i see how the concept works now dave, thank you...however, i don't see why that would make sense...if you have a setter method for the integer, why pass it in the constructor?
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Because you might want a certain initial value. If a class level variable is not initialized to a value in the constructor, by default it is set to 0, false or null depending on the type of the variable.
Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
gotcha, thanks again for the help dave!

Jay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing same objects in a class constructor and methods
 
Similar Threads
asking about interfaces
ArrayList, contains, objects
Sorted TreeSet from HashSet
SCJP5.0 passed!!
Generics - need help