Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do I get a class constant for a List

Posted on 2011-09-14
10
Medium Priority
?
281 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 375 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

782 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