| | |
Midlet Static Variable problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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
•
•
•
•
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.
Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: May 2008
Posts: 40
Reputation:
Solved Threads: 7
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.
======
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
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)
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 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.....<(^.^)>
![]() |
Similar Threads
- error LNK2001: unresolved external symbol - only happens with STATIC variable (C++)
- static variable got initialized everytime (PHP)
- Create static variable in non static member function (C++)
- Understanding Static Variable and Their Best Use: (C++)
- static variable (C)
- about static variable (Java)
Other Threads in the Java Forum
- Previous Thread: How to call other class file with button in java applet??
- Next Thread: Help needed please
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component csv database desktop draw ebook eclipse equation error event exception fractal game givemetehcodez graphics gui html ide image input integer intersect iphone j2me java java.xls javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions reporting rotatetext scanner screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





