943,691 Members | Top Members by Rank

Aug 31st, 2009
0

Midlet Static Variable problem

Expand Post »
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
Last edited by peter_budo; May 20th, 2010 at 5:30 pm. Reason: Adding code tags to old thread
Reputation Points: 10
Solved Threads: 0
Newbie Poster
musi77 is offline Offline
1 posts
since Aug 2009
Sep 1st, 2009
0

Re: Midlet Static Variable problem

Click to Expand / Collapse  Quote originally posted by musi77 ...
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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 2nd, 2009
0

Re: Midlet Static Variable problem

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.
java Syntax (Toggle Plain Text)
  1. public class abc {
  2.  
  3. static counter = 0;
  4. abc() { // Constuctor
  5. counter++;
  6. }
  7.  
  8. public static void main(String[] args) {
  9.  
  10. new abc();
  11. new abc();
  12. new abc();
  13.  
  14. System.out.print(counter);
  15. }
  16.  
  17. }
======
OUTPUT
======
3

==========
EXPLANATION
==========
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
Last edited by harsh2327; Sep 2nd, 2009 at 11:58 am.
Reputation Points: 27
Solved Threads: 8
Light Poster
harsh2327 is offline Offline
44 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Mobile Development Forum Timeline: J2ME: Display image from filesystem
Next Thread in Mobile Development Forum Timeline: change form display j2me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC