The Artima Developer Community
Sponsored Link

Java Answers Forum
Class class, array of objects ...

2 replies on 1 page. Most recent reply: May 20, 2003 1:32 PM by Jaycee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Albrecht Mueller

Posts: 4
Nickname: lbrtchx
Registered: Feb, 2003

Class class, array of objects ... Posted: May 19, 2003 9:12 AM
Reply to this message Reply
Advertisement
I need to ("command pattern"-like) call classes which names I know to create
certain objects.

Then the objects (kept in an array) should call the same name methods (doing
something different for different objects, etc.) So you will be effectively
calling an object's method based on an index.

I thought something like that you could do in Java easily, but apparently
there may be something I might be missing here.

Short code examples of both classes with comments are included.

How can you do this?

// - - - - - - - - - - - - - - - -- - - - - - -

import java.util.*;

class K0{
String azKNm;
int iIx;
// __
public K0(){
this.azKNm = "K0";
this.iIx = 0;
}
// __
public String doIt(){
// __ An exception is simulated and caught to look at the call Stack
(new Exception((new Date()).toString())).printStackTrace();
// __ you do return the internal value, though
return(toString());
}
// __
public String toString(){ return(azKNm + "|" + iIx); }
}

// - - - - - - - - -

public class TestKn04{
public static void main(String[] azArgs){
K0 K;
Class KX = null;
Object Obj = null;

// __ This, of course :-), works
K = new K0();
System.err.println("- - - - - - - - - - - - - - - - - -");
System.err.println("K.doIt()");
System.err.println(K.doIt());


// __ Works fine too
try{ KX = Class.forName("K0"); }
catch(ClassNotFoundException KNFX){ KNFX.printStackTrace(); }

try{
Obj = KX.newInstance();
System.err.println("- - - - - - - - - - - - - - - - - -");
System.err.println("((K0)Obj).doIt()");
System.err.println(((K0)Obj).doIt());
}catch(InstantiationException InstX){ InstX.printStackTrace(); }
catch(IllegalAccessException IlglX){ IlglX.printStackTrace(); }


// __ Why doesn't this work?
int i;
int iTtlKs = 1;
Class[] KAr = new Class[iTtlKs];
Object[] ObjAr = new Object[iTtlKs];

i = 0;
try{ KAr = Class.forName("K" + i); }
catch(ClassNotFoundException KNFX){ KNFX.printStackTrace(); }

try{
ObjAr = KAr.newInstance();
System.err.println("- - - - - - - - - - - - - - - - - -");

System.err.println(" K's class is " + K.getClass().getName());
System.err.println(" ObjAr's class is " + ObjAr.getClass().getName());

System.err.println();

System.err.println(" (Obj instanceof K0) " + (Obj instanceof K0));
System.err.println(" (ObjAr instanceof K0) " + (ObjAr instanceof K0));

// __ So far so good!

System.err.println();

// __ This below does not work
// System.err.println(" (Obj instanceof KAr) " + (Obj instanceof KAr));
// System.err.println(" (ObjAr instanceof KAr) " + (ObjAr instanceof KAr));

System.err.println("- - - - - - - - - - - - - - - - - -");
System.err.println("((K0)ObjAr).doIt()");
System.err.println(((K0)ObjAr).doIt()); // This does work fine!!!

// __ The examples below do not even compile, even if KAr and K0 are objects
// of the same type.

// ((KAr)ObjAr).doIt();

// ((ObjAr.getClass().getName())ObjAr).doIt();

// ((Class.forName(ObjAr.getClass().getName()))ObjAr).doIt();

}catch(InstantiationException InstX){ InstX.printStackTrace(); }
catch(IllegalAccessException IlglX){ IlglX.printStackTrace(); }
}
}


Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Class class, array of objects ... Posted: May 20, 2003 1:29 PM
Reply to this message Reply
Your array code is not displaying properly. When you use 'i' as the index for an array, Artima formatting views that as an italic tag.

Post again, using
 your code here 
.

Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Class class, array of objects ... Posted: May 20, 2003 1:32 PM
Reply to this message Reply
;)

I mean, <java>your code here</java> .

Replace the greater/lesser than signs with square brackets.

Flat View: This topic has 2 replies on 1 page
Topic: Singleton vs Class with static methods Previous Topic   Next Topic Topic: System testing

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use