Solved

how do I get a class constant for a List

Posted on 2011-09-14
10
270 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse compiler vs Installed JREs option 3 75
Checkbox and ListView in Android Layout 4 56
Problem to start Neon 20 86
Apps blocked by Java 9 60
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

910 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

17 Experts available now in Live!

Get 1:1 Help Now