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

print a parameter name and value (with a function)

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reflection?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is not clear.....
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct; there's no way to know the name of a parameter at runtime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic