PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How can I pass field name of a class as parameter to include it in a String?

 July 07, 2022     class, field, java, string     No comments   

Issue

I'm wondering How can I pass field name of a class as parameter to include it in a String?

For example, let's say I have a class A with this members :

public class A {

String name;
String reference;
....

}

What I want, is to be able to initialize a string having as parameters one of the two classe field name to display it dynamically. So for instance, I want to do this :

System.out.println("the field {nameField} belong to class A.");

And in output I would have this :

output : the field name belong to class A.

Is that possible in Java ?

Any help would be much appreciated ?

Regards

YT


Solution

This can be done by use of reflection in Java. https://www.oracle.com/technical-resources/articles/java/javareflection.html#:~:text=Reflection%20is%20a%20feature%20in,its%20members%20and%20display%20them.



Answered By - Harshita
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing