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 6877437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:36:02+00:00 2026-05-27T04:36:02+00:00

I frequently use constants defined like: private static final int myConstant_x = 1; private

  • 0

I frequently use constants defined like:

private static final int myConstant_x = 1;
private static final int myConstant_y = 2;

private int anyVariable;
anyVariable = myConstant_x

during debugging it would be really helpful to somehow access the name of the variable for log output instead of the value.

Log.i (TAG,"this is debug output of anyVariable with constant name: " + ????);

with output:
“this is debug output of anyVariable with constant name: myConstant_x”

instead of

Log.i (TAG,"this is debug output of anyVariable with constant name: " + anyVariable);

with output:
“this is debug output of anyVariable with constant name: 1”

Is there any way?

I know one can get the method name from within code but is there a way to get the variable name too somehow?

Would be really helpful

Thanks

EDIT: updates/corrected the example code – sorry for the first misleading version

  • 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-05-27T04:36:03+00:00Added an answer on May 27, 2026 at 4:36 am

    You could get the fields of a class using reflection and iterate over them. You’d then have access to the name and the value.

    Something like this:

    public void logStaticFields( Class<?> clazz) throws IllegalAccessException {
      for( Field f : clazz.getDeclaredFields() ) {
        if( Modifier.isStatic( f.getModifiers() ) ) {
          boolean wasAccessible = f.isAccessible();
          f.setAccessible(true);
          Log.i (TAG, "this is debug output of static field " + f.getName() + ": " + f.get( null ) );
          f.setAccessible( wasAccessible );
        }
      }
    }
    

    If you want to only log constants (which normally are static final) you could check for Modifier.isStatic( f.getModifiers() ) && Modifier.isFinal( f.getModifiers() ).

    This method also temporarily disables access checks, otherwise get() might throw that IllegalAccessException. Note that this might still not work, depending on your JVM’s SecurityManager configuration.

    Note that this only works for fields (class or instance variables) and method parameters, not for local variables within a method since theres no reflection information for those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

People use frequently something like: <ListBox ItemsSource={Binding ElementName=thisControl, Path=ListIndexes}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Label Content={Binding
I use temp tables frequently to simplify data loads (easier debugging, cleaner select statements,
If you frequently use in your program a collection which path would you prefer:
I frequently use git stash and git stash pop to save and restore changes
I frequently use HTML output in applications, up to now I've used some simple
I frequently use the following pattern to set an upper bound to the running
I use Visual C++ 2008 in Visual Studio 2008. I frequently use the following
Hi I have SVN repo that I frequently use for change management. However after
I am using NetBeans for PHP 6.5. In my code I frequently use the
I frequently make use of Request.QueryString[] variables. In my Page_load I often do things

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.