S
stan k.
I am trying to use a singleton (global variable class) in which one of
the properties is
a List of innerclasses. My first question is should I be using a
Collection instead of
an ArrayList?
I am able to add each instance of my inner class to the arraylist
without error but it doesn't
seem to be retaining the individualized values of the innerclass and
secondly I am not able
to extract the innerclass values from teh list - regardless of what
i've tried...
I've tried casting, using myList.toArray() etc... Please help I am
totally stuck !
Here is the code below.......
import java.io.*;
import java.util.*;
class globals{
private static globals g = null;
public static InnerClass ic = null;
private static int COUNT=0;
private globals(){}
public List myList = new ArrayList();
public static class InnerClass {
public static String prop3 = null;
public static String prop4 = null;
}
///////////////////////////////////////////////
public static globals getInstance(){
if ( g == null ){
g = new globals();
ic = new InnerClass();
COUNT++;
}
if ( ic == null ){
globals.InnerClass ic = new globals.InnerClass();
System.out.println("ic instantiated from null");
}else{
System.out.println("ic instantiated earlier");
}
System.out.println("globals() has been instantiated
"+COUNT+"times."); //always returns COUNT=1
return g ;
}//getInstance()
public void pushInnerClass(){
if ((ic.prop3 != null) && (ic.prop4 != null)) {
System.out.println("pushing ic with vals:" + ic.prop3+ic.prop4);
myList.add(ic);
clearICplaceholders();
}else{
System.out.println("****error **** ic's props not filled yet with
values. please insert values before pushing");
};
}
public void clearICplaceholders(){
ic.prop3=null;
ic.prop4=null;
}
}//class globals()
public class whatever{
private static globals g = null;
private static whatever nv = null;
public whatever(){
for(int i=0; i<4; i++){
g = globals.getInstance();
g.ic.prop3=("newvalp3 "+i);
g.ic.prop4=("newvalp4 "+i);
g.pushInnerClass();
}//for
}//whatever method()
public static void main(String[] str){
nv = new whatever();
}
}//whatever class
the properties is
a List of innerclasses. My first question is should I be using a
Collection instead of
an ArrayList?
I am able to add each instance of my inner class to the arraylist
without error but it doesn't
seem to be retaining the individualized values of the innerclass and
secondly I am not able
to extract the innerclass values from teh list - regardless of what
i've tried...
I've tried casting, using myList.toArray() etc... Please help I am
totally stuck !
Here is the code below.......
import java.io.*;
import java.util.*;
class globals{
private static globals g = null;
public static InnerClass ic = null;
private static int COUNT=0;
private globals(){}
public List myList = new ArrayList();
public static class InnerClass {
public static String prop3 = null;
public static String prop4 = null;
}
///////////////////////////////////////////////
public static globals getInstance(){
if ( g == null ){
g = new globals();
ic = new InnerClass();
COUNT++;
}
if ( ic == null ){
globals.InnerClass ic = new globals.InnerClass();
System.out.println("ic instantiated from null");
}else{
System.out.println("ic instantiated earlier");
}
System.out.println("globals() has been instantiated
"+COUNT+"times."); //always returns COUNT=1
return g ;
}//getInstance()
public void pushInnerClass(){
if ((ic.prop3 != null) && (ic.prop4 != null)) {
System.out.println("pushing ic with vals:" + ic.prop3+ic.prop4);
myList.add(ic);
clearICplaceholders();
}else{
System.out.println("****error **** ic's props not filled yet with
values. please insert values before pushing");
};
}
public void clearICplaceholders(){
ic.prop3=null;
ic.prop4=null;
}
}//class globals()
public class whatever{
private static globals g = null;
private static whatever nv = null;
public whatever(){
for(int i=0; i<4; i++){
g = globals.getInstance();
g.ic.prop3=("newvalp3 "+i);
g.ic.prop4=("newvalp4 "+i);
g.pushInnerClass();
}//for
}//whatever method()
public static void main(String[] str){
nv = new whatever();
}
}//whatever class