i have a midlet which has got a static variable. i need to keep the record of all the instances created in this variable. but it does not work like static variable. my code segments look like this. i am running this midlet on sun wireless toolkit 2.5.5. i can create many objects of same midlet from that toolkit but still my counter shows only 1.

public class SMS extends MIDlet implements CommandListener {

private Display display; private TextField userID, password ; public static int counter ;

public SMS() {

  userID = new TextField("LoginID:", "", 10, TextField.ANY);
  password = new TextField("Password:", "", 10, TextField.PASSWORD);


public void startApp() {

  display = Display.getDisplay(this);

public void commandAction(Command c, Displayable d) {

 String label = c.getLabel();
 System.out.println("Total Instances"+counter);

everytime, counter shows only 1 object created. please, help me out

i am running this midlet on sun wireless toolkit 2.5.5. i can create many objects of same midlet from that toolkit ....


Firstly, read the rules before you post. You should post code inside the CODE tags.

Im not sure what you mean by creating objects from toolkit, Problem could be that toolkit do not create new object everytime. Its just passing you the reference of Object created on first time.

You can check it by simply adding " System.out.println("Checking") " to constructor. If "Checking" is printed only one time on Console.


Member Avatar

Running multiple copies of your code implies that you are creating an entirely new process.
Its like running 2 independent processes (like 2 IEs or 2 notepads executing at once). These two processes should be totally independent.

Similarly, you are creating two different MIDlets and obviously you will have two different "counters" and hence it will always show as 1.

I believe you have misunderstood the definition of "static variables". Consider the following example.

public class abc {

	static counter = 0;
	abc() {			// Constuctor

	public static void main(String[] args) {

		new abc();
		new abc();
		new abc();




Here, the counter is static. Moreover, it is present in the same process. If you run two different instances of the same process, they both will have different counters. You cannot count the number of instances using "static variables"

Also, majority of the cellphones will not support executing multiple copies of the same program.

Hope I was able to explain properly