Solved

how do I get a class constant for a List

Posted on 2011-09-14
10
271 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 194
Custom Java Application connects to database: Which one of the 20? 4 28
jboss wildfly 10.1 10 94
Java: anonymous class 4 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

778 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