aspose file tools*
The moose likes Beginning Java and the fly likes print a parameter name and value (with a function) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print a parameter name and value (with a function)" Watch "print a parameter name and value (with a function)" New topic
Author

print a parameter name and value (with a function)

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 247
Is there a way to print a parameter name and value (with a function)?
e.g. with the following code, how to define printvar to get the same output?


Output:
i 10
name John
i 10
name John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Put the code in a method called "printvar"?

I guess I don't understand the question--are you just asking how to create a Java method? Moving to beginning Java.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

reflection?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Seetharaman Venkatasamy wrote:reflection?

Surely there's an easier way to print a string and a number than using reflection. Let's give the original poster a chance to explain what they're asking and attempt some code first.

But I sure don't see anything requiring reflection yet.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3669
    
  15
I think he's asking whether if he passes a variable to a method, that method can print out the name of the variable and its value
i.e. printvar(i) will print out the name of the variable (i) and the value of the variable (10) as i = 10.

The answer is no, because Java is pass by value. What actually gets passed to the method is the value 10. The method knows nothing about the variable i.

Joanne
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Ah, that makes more sense now. But yeah, the answer is no :) You'd need to pass the name of the variable or come up with some other mechanism.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Question is not clear.....


SCJP6.0,My blog Ranchers from Delhi
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 247
Joanne Neal wrote:I think he's asking whether if he passes a variable to a method, that method can print out the name of the variable and its value
i.e. printvar(i) will print out the name of the variable (i) and the value of the variable (10) as i = 10.

The answer is no, because Java is pass by value. What actually gets passed to the method is the value 10. The method knows nothing about the variable i.


Thank you for your understanding of my question.
Your answer means that both the name of the variable and its value are needed for the function parameters:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Correct; there's no way to know the name of a parameter at runtime.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: print a parameter name and value (with a function)