Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing same objects in a class constructor and methods

 
Jay Peigh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes its same. Constructor is when you initiate objects. Methods can be called any time.

Regards,
Nalaka
http://javatouch.googlepages.com
 
Jay Peigh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jay Peigh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this is a better example. This is a very simple mutable integer wrapper class.

 
Jay Peigh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotcha, thanks again for the help dave!

Jay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic