Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8511087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:53:26+00:00 2026-06-11T03:53:26+00:00

I need to make a method that takes the name of a variable as

  • 0

I need to make a method that takes the name of a variable as its input, searches the class, and outputs the value pointed to by that variable. However, as you might have guessed, I am having problems with it:

public String myMethod(String varname) {
    return varname;
}

As you can see, all the above does is return the input string. Is there any way to get it to do otherwise?

Edit: for those requesting an explanation, here it is:

Suppose I have the following code:

String foo = "foo";
String bar = "bar";

I want to make a method that takes the name of a variable and returns its value. So, if I used the above method and wrote myMethod(foo), the output would be “foo”; similarly, I want myMethod(bar) to give “bar”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:53:28+00:00Added an answer on June 11, 2026 at 3:53 am

    Use the Java Reflection API. For example:

    private String foo = "foo text";
    private String bar = "some text";
    
    public String myMethod(String varname) throws Exception {
      Class<?> c = this.getClass();
      Field field = c.getDeclaredField(varname);
      Object fieldValue = field.get(this);
      return fieldValue.toString();
    }
    
    // myMethod("foo") returns "foo text"
    // myMethod("bar") returns "some text"
    

    DEMO.

    References:

    • The Reflection API
    • Getting and Setting Field Values
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have method in a class that I need to make sure is only
I have 3 buttons to change language. So I need to make method, where
I have a BSjax class that I wrote that lets me make async calls
Consider this example: class MyClass: def func(self, name): self.name = name I know that
I need to create an app or game inside of an app that takes
I want to make a generic method that will take either a single object
I'm using argspec in a function that takes another function or method as the
I'm trying to make upload site that takes request and stores the files in
I want to create a class that can take different types of value in
I'm implementing a method in C# that takes a single argument of type object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.