• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

invoke varible by number


public class AA
{
         public String _1_;
         public String _2_;
         public String _3_;

         /** this is question */
        main()
        {
                    for( int i = 0 ; i< 3 ; i++)
                    {
                                String value = getVariable( "_" + i + "_"); <<<<<<<<<<<< how can I invoke global variable by String??
                     }
               
        }
}

0
gamjaradio
Asked:
gamjaradio
8 Solutions
 
CEHJCommented:
You can't really. The best you can do is to use a String as the key into a Map in which you store the variable as the value
0
 
CEHJCommented:
e.g.
import java.util.*;

public class M {
    static String string1 = "A";

    public static void main(String[] args) {
	Map<String, String> m = new HashMap<String, String>();
	m.put("1", string1);
	System.out.println(m.get("1"));
    }
}

Open in new window

0
 
enachemcCommented:
you can do it using reflection, but I'm prety sure this is not the way to do it
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ksivananthCommented:
you can do it through reflection, check this example

http://www.java2s.com/Code/Java/Language-Basics/ObjectReflectiongetfieldvalue.htm
0
 
dpearsonCommented:
If you really want to do this try something like:

           public main() throws Exception {            
            for (int i  = 1 ; i <= 3 ; i++) {
                  String name = "_" + i + "_" ;
                  Field field = AA.class.getField(name) ;
                  System.out.println("Value of " + name + " is " + field.get(this)) ;
            }
          }

but you are probably better off storing the values in variables that are easier to access.

Doug
0
 
for_yanCommented:

That is how it works through reflection:

import java.lang.reflect.Field;

public class AA
{
         public String _1_;
         public String _2_;
         public String _3_;

    public AA(){
        _1_ = "ttt";
        _2_ = "aaa";
        _3_ = "bbb";
    }

         /** this is question */
     public static void  main(String [] args)
        {
            AA aa = new AA();

                     try{
                Class cls = aa.getClass();
                         Field fieldlist[]
                                      = cls.getDeclaredFields();


                         
                         
                    for( int i = 0 ; i< 3 ; i++)

                    {
                        System.out.println(fieldlist[i].getName().toString());
                        String s =   fieldlist[i].getName().toString().trim();
                        String s1 =  ("_" + (i+1) + "_").trim();



                        if(s.equals(s1)){
          
                            System.out.println((String)fieldlist[i].get(aa));
                       // }
                        }
                                //String value = getVariable( "_" + i + "_"); <<<<<<<<<<<< how can I invoke global variable by String??
                     }

        }
        catch(Exception ex){
            System.out.println("Error " + ex.toString());
            ex.printStackTrace();

}
}
}

Open in new window

0
 
for_yanCommented:

Thsi is the output of the above code;

_1_
ttt
_2_
aaa
_3_
bbb

Open in new window

0
 
for_yanCommented:

This is a little bit shorter version of the above
(the most difficulty and time consuming part as always was not to forget
to add 1 in (i+1) when doing comparison:

import java.lang.reflect.Field;

public class AA
{
         public String _1_;
         public String _2_;
         public String _3_;

    public AA(){
        _1_ = "ttt";
        _2_ = "aaa";
        _3_ = "bbb";
    }

         /** this is question */
     public static void  main(String [] args)
        {
            AA aa = new AA();

                     try{
                Class cls = aa.getClass();
                         Field fieldlist[]
                                      = cls.getDeclaredFields();


                         
                         
                    for( int i = 0 ; i< 3 ; i++)

                    {
  

                        if(fieldlist[i].getName().equals("_" + (i+1) + "_")){
          
                            System.out.println((String)fieldlist[i].get(aa));
                      
                        }
                                //String value = getVariable( "_" + i + "_"); <<<<<<<<<<<< how can I invoke global variable by String??
                     }

        }
        catch(Exception ex){
            System.out.println("Error " + ex.toString());
            ex.printStackTrace();

}
}
}

Open in new window


Output:

ttt
aaa
bbb

Open in new window

0
 
CEHJCommented:
gamjaradio, what is your actual goal here? In a real program you would never normally use a Map for this purpose, and still less use reflection.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now