Solved

how do I get a class constant for a List

Posted on 2011-09-14
10
272 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 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