954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Midlet Static Variable problem

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);
  counter++;

}

public void startApp() {

  display = Display.getDisplay(this);
  loginForm.append(userID);
  loginForm.append(password);
  loginForm.addCommand(cancel);
  loginForm.addCommand(login);
  loginForm.setCommandListener(this);
  display.setCurrent(loginForm);

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

musi77
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
i am running this midlet on sun wireless toolkit 2.5.5. i can create many objects of same midlet from that toolkit ....

Hello,

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.

Regards,

puneetkay
Junior Poster
122 posts since Nov 2007
Reputation Points: 51
Solved Threads: 24
 

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
		counter++;
	}

	public static void main(String[] args) {

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

		System.out.print(counter);
	}

}

======
OUTPUT
======
3

==========
EXPLANATION
==========
Here, the counter is static. Moreover, it is present in thesame 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

harsh2327
Junior Poster in Training
56 posts since May 2008
Reputation Points: 27
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You