Midlet Static Variable problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 1
Reputation: musi77 is an unknown quantity at this point 
Solved Threads: 0
musi77 musi77 is offline Offline
Newbie Poster

Midlet Static Variable problem

 
0
  #1
Aug 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Midlet Static Variable problem

 
0
  #2
Sep 1st, 2009
Originally Posted by musi77 View Post
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 40
Reputation: harsh2327 is an unknown quantity at this point 
Solved Threads: 7
harsh2327 harsh2327 is offline Offline
Light Poster

Re: Midlet Static Variable problem

 
0
  #3
Sep 2nd, 2009
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.
  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.
<(^.^)>.....HM.....<(^.^)>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC