File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Passing a refrence to another Class vs method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing a refrence to another Class vs method" Watch "Passing a refrence to another Class vs method" New topic
Author

Passing a refrence to another Class vs method

Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
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
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

Meaning, something like this will work...



Henry
Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

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

Joined: Nov 05, 2008
Posts: 50

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing a refrence to another Class vs method