Problem: global variable with a 2d dynamic array simulated using a collection class within a singlet

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
 
J

John C. Bollinger

stan said:
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?

An ArrayList _is_ a Collection, so your first question is nonsensical.
Moreover, Collection is an interface; you have to choose a particular
implementation (such as ArrayList or HashSet). You seem to want to
create a stack-like structure; a List of some sort is appropriate for
that. May I suggest, however, the Stack class (a List implementation)?
Stack is based on the older Vector class, which is synchronized, but
synchronization is probably what you want here anyway.
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

That's because there aren't any individualized values. These would be
represented by instance variables, but your inner class contains only
class (static) variables. Class variables are shared by all instances.

Moreover, I don't see where you ever create more than one instance of
the inner class in the first place.
secondly I am not able
to extract the innerclass values from teh list - regardless of what
i've tried...

Well, I don't see any code to accomplish this, so I really can't comment
much on it. There is nothing particularly special about the operation.
It may work out more naturally if you switch to an internal Stack.
Note that you will still need to cast the objects you pop off the Stack
(or retrieve from the end of the list, or whatever) to the appropriate type.
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{

Use standard naming conventions. Class names should begin with capital
letters. This makes it much easier for others to follow what is going on.

Also, the Globals class might need to be public. It certainly must be
final to correctly implement the Singleton pattern.
private static globals g = null;
public static InnerClass ic = null;

Should ic really be public? Is it okay if a user replaces it with a
different instance, or nullifies it? Even in a multi-threaded scenario?
If the answer is "no", then make it private and provide an accessor
method.
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();

You may refer to InnerClass that way here, but it is not necessary to
qualify with the containing class name inside the containing class. It
is, in fact, a practice I personally dislike.
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()

If you're going to write getInstance() at all along those lines then it
must be synchronized. Better, however, would be to initialize g (which
would be better named "instance") in an initialization statment in the
declaration. I really don't see what you are trying to accomplish with
your inner class instances, but that code does not belong here --
getInstance should not modify the state of the instance.
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;
}

Thus you re-use the same InnerClass instance, and even switching from
class to instance variables will not give you distinct sets of values on
your stack. More likely you want something like ic = new InnerClass()
instead of this clearICplaceholders method. And is it in fact
appropriate to expose that method to the public? In any case, if a
method to do this is necessary then it probably belongs on InnerClass,
and not on globals.
}//class globals()


public class whatever{

Use standard naming conventions.
private static globals g = null;
private static whatever nv = null;

public whatever(){
for(int i=0; i<4; i++){
g = globals.getInstance();

Since globals is a Singleton, why do you repeatedly retrieve the same
instance?
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

You seem to be having trouble with several distinct concepts, all
muddled together in your code. You should work them out individually
before trying to put them together:

(1) The Singleton pattern. [How to correctly and safely implement it.]
(2) Class variables vs. instance variables.
(3) The meaning of "static", and perhaps the fact that that meaning is
different when applied to class declarations than when applied to method
or field declarations.
(4) The difference between classes and objects (instances).
(5) Encapsulation.
(6) Synchronization. (Its necessity being implied by the fact that you
are using a Singleton. There is no point in going through the hassle of
a Singleton if the one instance isn't going to be shared.)

I have attempted to address most of these issues to one degree or
another in my comments, but they are by no means a thorough treatment.


Regards,

John Bollinger
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top