Trying to create a generic array

Discussion in 'Java' started by lbrtchx@gmail.com, May 1, 2008.

  1. Guest

    I am getting an "incompatible types" compile time error, even though
    to me (and apparently to the compiler too) these are just three forms
    to refer to the same class:

    1) ai.class;
    2) Class.forName("ai");
    3) ((Class.forName("ai")).newInstance()).getClass();

    Here is the actual code example:

    // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    import java.util.*;
    import java.lang.reflect.*;

    class ai{
    String a;
    int i;
    }

    class gnrx10{
    gnrx10(){}

    public <T> T[] createTypeAr(Class<T> Tp, int iSz){
    T[] TpAr = (T[])Array.newInstance(Tp, iSz);
    return(TpAr);
    }
    }

    public class gnrx10Test{
    public static void main(String args[]){
    gnrx10 g = new gnrx10();

    // __ this works fine!
    String[] aSAr = g.createTypeAr(String.class, 4);
    System.out.println("// __ aSAr.length: |" + aSAr.length + "|");

    ai[] aiAr = g.createTypeAr(ai.class, 16);
    System.out.println("// __ aiAr.length: |" + aiAr.length + "|");
    // __
    try{

    System.out.println(ai.class);
    System.out.println(Class.forName("ai"));
    System.out.println(((Class.forName("ai")).newInstance()).getClass());

    // __ this doesn't!
    /*
    Class K = ai.class;
    System.out.println(K);
    aiAr = g.createTypeAr(K, 8);
    System.out.println("// __ aiAr.length: |" + aiAr.length + "|");

    gnrx10Test.java:47: incompatible types
    found : java.lang.Object[]
    required: ai[]
    aiAr = g.createTypeAr(K, 8);
    ^
    */

    }catch(ClassNotFoundException KNFX){ KNFX.printStackTrace(); }
    catch(InstantiationException InstX){ InstX.printStackTrace(); }
    catch(IllegalAccessException IlgAxX){ IlgAxX.printStackTrace(); }
    }
    }
    , May 1, 2008
    #1
    1. Advertising

  2. Guest

    OK, all these cases work fine:

    Class<KAI> K00 = KAI.class;

    Class<KAI> K02 = (Class<KAI>)Class.forName("KAI");

    Class<? extends KAI> K04 = (Class<KAI>)Class.forName("KAI");

    Class<? extends KAI> K06 = (Class<? extends
    KAI>)Class.forName("KAI");

    But what I am really trying to do is just do it based on a string
    declaring the name of the class, namely "KAI", without using type
    declarations "Class<KAI>"-like in the code

    1) Say you gain some info about a DTO you will use, then

    2) you use reflection to actually make such an object, say "KAI"

    3) you save then the class in a reachable place and load a second
    class which will be the one using "KAI" objects

    How could you do that?

    lbrtchx
    , May 1, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,028
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    424
  3. Andrea Desole

    Why can't I create a generic array?

    Andrea Desole, Sep 6, 2006, in forum: Java
    Replies:
    3
    Views:
    8,957
  4. minlearn
    Replies:
    2
    Views:
    445
    red floyd
    Mar 13, 2009
  5. Warren Tang
    Replies:
    8
    Views:
    5,112
    Warren Tang
    Oct 21, 2011
Loading...

Share This Page