Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing a refrence to another Class vs method

 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello thanks for your time,

How come when I pass a copy of a refernce to a method, I don't have to create a new type of the refernce inside the method and set it equal to the name of the refrence?

It won't compile this way,





It does compile this way;

 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't compile this way,




The class2 parameter to the constructor, is local to the constructor. It goes out of scope, once the constructor finishes. It is certainly not in scope during the methodOne() call.

If you want the methodOne() method to have access to the object passed in the constructor, perhaps you should consider saving it -- maybe in an instance variable.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meaning, something like this will work...



Henry
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't understand why it would be local to the constructor,
but that most certainly is the way it acts. So if I understand it correctly
while a constructor helps build the class, it's scope is limited to the duration of the construction phase.

Thanks for your time;
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chadd Franck:
I couldn't understand why it would be local to the constructor,
but that most certainly is the way it acts. So if I understand it correctly
while a constructor helps build the class, it's scope is limited to the duration of the construction phase.


Names declared as constructor parameters or local variables in a constructor are all local to the constructor. In this sense, constructors are exactly like any other method. Why is this surprising?
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

Names declared as constructor parameters or local variables in a constructor are all local to the constructor. In this sense, constructors are exactly like any other method. Why is this surprising?



Agreed. This has nothing to do with constructors. Methods can break too, if you try to use something that is not in scope. For example, this won't compile either...



Henry
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Names declared as constructor parameters or local variables in a constructor are all local to the constructor. In this sense, constructors are exactly like any other method. Why is this surprising?


Because if you pass a parameter to a method the parameter lasts for the life of the method, so when you pass a parameter as an argument to a class I thought it should pass a parameter to the class that would be used for the life of the class. I see now that it doesn't, I thought the constructor was used as a workaround to allow the class have the same scope
parameters of the method. I see that it can still be used this way but you have to create a objRefernce or a new object, and just set this parameter in the constructor. (probably with a this. constructor).

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic