|
How to pass reference in a method |
|
|
You should be reasonably comfortable with the idea that when you’re “passing” an object, you’re actually passing a reference.
A sample experiment demonstrates this:
public class References
{
static void f(References h) {
System.out.println("h inside f(): " + h);
}
public static void main(String[] args) {
References p = new References();
System.out.println("p inside main(): " + p);
f(p);
}
}
|
The method toString() is automatically invoked in the print statements, and References inherits directly from Object with no redefinition of toString(). Thus, Object’s version of toString() is used, which prints out the class of the object followed by the address where that object is located (not the reference, but the actual object storage). The output looks like this:
p inside main(): References@108786b
h inside f(): References@108786b
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.