• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

print a parameter name and value (with a function)

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