Solved

how do I get a class constant for a List

Posted on 2011-09-14
10
269 Views
Last Modified: 2012-05-12
I need to get the class constant for a List in java
like

String.class

but for
List<String>

how do I do this?

0
Comment
Question by:lfrohman
  • 5
  • 5
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36538199

waht you mean by class constant for a List - can you elaborate?
0
 

Author Comment

by:lfrohman
ID: 36538219
like

Class c = String.class;

I want

Class d = xxx.class

to give me the same thing as
Class d = (new ArrayList<String>()).getClass();

(except this would result in ArrayList<String>, not List<String>)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36538249

Sorry still don't understand.

    For this:
                 Class d = (new ArrayList<String>()).getClass();
                          System.out.println(d.getName());

   I got the output:

java.util.ArrayList

So it makes sense; what do you want to get?

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36538279


                             Class d = (new ArrayList<String>()).getClass();
                          System.out.println("for d: " + d.getName());
             Class d1 = ArrayList.class;
          System.out.println("for d1: " + d1.getName());
            Class d2 = String.class;
          System.out.println("for d2: " + d2.getName());

Open in new window


output:
for d: java.util.ArrayList
for d1: java.util.ArrayList
for d2: java.lang.String

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36538366
Maybe you want to return the type name for the generic type.

It can be done this way (see the last part of the code, and the last line of output):

public class Miscellaneous {

  public  static ArrayList<String> ars;

    public static void main(String[] args) {


      

                             Class d = (new ArrayList<String>()).getClass();
                          System.out.println("for d: " + d.getName());
             Class d1 = ArrayList.class;
          System.out.println("for d1: " + d1.getName());
            Class d2 = String.class;
          System.out.println("for d2: " + d2.getName());

        try{
  Field field =Miscellaneous.class.getField("ars1");
  Type genericFieldType = field.getGenericType();
            System.out.println("type: " + genericFieldType);
        }catch(Exception ex){
          ex.printStackTrace();  
        }

}
}

Open in new window


output:

for d: java.util.ArrayList
for d1: java.util.ArrayList
for d2: java.lang.String
type: java.util.ArrayList<java.lang.String>

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 36538377
Small correction in the code above;
this one is actually working:

public class Miscellaneous {

  public  static ArrayList<String> ars;

    public static void main(String[] args) {


      

                             Class d = (new ArrayList<String>()).getClass();
                          System.out.println("for d: " + d.getName());
             Class d1 = ArrayList.class;
          System.out.println("for d1: " + d1.getName());
            Class d2 = String.class;
          System.out.println("for d2: " + d2.getName());

        try{
  Field field =Miscellaneous.class.getField("ars");
  Type genericFieldType = field.getGenericType();
            System.out.println("type: " + genericFieldType);
        }catch(Exception ex){
          ex.printStackTrace();  
        }

}
}

Open in new window


output::

for d: java.util.ArrayList
for d1: java.util.ArrayList
for d2: java.lang.String
type: java.util.ArrayList<java.lang.String>

Open in new window


0
 

Author Comment

by:lfrohman
ID: 36539820
Never mind, I needed an array, not a list, and I can use
String[].class
0
 

Author Comment

by:lfrohman
ID: 36539833
I've requested that this question be closed as follows:

Accepted answer: 0 points for lfrohman's comment http:/Q_27308291.html#36539820

for the following reason:

xxx
0
 

Author Comment

by:lfrohman
ID: 36539834
xxx
0
 

Author Closing Comment

by:lfrohman
ID: 36539836
xxx
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now