Your first J2ME program and a midlet lifecycle explained.

By Grenfel Viewed: 31807 times Emailed: 268 times Printed: 311 times Bookmark and Share



As a programmer, you probably know that the first program you ever write when you are learning a new language is displaying the famous words, 'Hello World". This was the first program that the founders of the great C language introduced when they introduced the C language to the world.

Here is the MIDP version of the inveterate "Hello World" program. The following J2ME code sample shows the source code for the first version of the HelloWorld MIDlet.

//This is the MIDP version of the familiar HelloWorld program.
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
/**
Creates the "Hello world" program in J2ME MIDP.
Note that the class must be public so that the device
application management software can instantiate it.
*/
public class HelloWorld extends MIDlet
 


{
// The Displayable. This component is displayed on the
// screen.
private Form form;
// The Display. This object manages all Displayable
// components.
private Display display;
// A public no-arg constructor is necessary, even though
// the system calls startApp()! The AMS calls the
// class's no-arg constructor to instantiate the class.
// Either create a public no-arg constructor, or declare
// no constructors, and let the compiler create a public
// no-arg constructor.
//
public HelloWorld()
{
super();
}
public void destroyApp(boolean destroy)
{
form = null;
notifyDestroyed();
}
public void pauseApp()
{
}
public void startApp()
{
// Create a Displayable widget.
form = new Form("Hello, World");
// Add a string to the form.
String msg = "My first MIDlet!";
form.append(msg);
// This app simply displays the single form created
// above.
display = Display.getDisplay(this);
display.setCurrent(form);
}
}

First, notice that this application defines a class called HelloWorld, which extends javax.microedition.midlet.MIDlet. All MIDlets must extend this class. The HelloWorld class is the primary class of your application. For this reason, it must be declared public. Moreover, you must declare a public no-argument constructor, or ensure that there are no constructors, in which case the compiler will define a no-argument constructor for you. Readers who are familiar with Java applets will recognize the similarity between the applet and MIDlet lifecycle control models.
 

In the program above, the startApp(), pauseApp(), and destroyApp() methods override abstract declarations from the MIDlet class. Notice that all the initialization code goes in the startApp() method rather than in the constructor. You certainly can put some initialization code in your constructor; it will be executed before the call to startApp(). However, the startApp() method is always called as the entry point for your MIDlet.

What about a main() method? The Java language definition requires all Java applications to have a main() method with the following signature:

public static void main(String [] args)

If J2ME applications are real Java applications, then there must be a main method somewhere that is the real entry point used by the VM to start the process of executing the application. In fact, there is such a method. It's part of the MIDP implementation (not the application), and, typically, the AMS software calls it. The AMS handles application invocation requests, for instance, by spawning a thread for each MIDlet startup request and controlling the MIDlet from that thread. Actual details are implementation dependent. In Sun's J2ME Wireless Toolkit, the class com.sun.midp.Main defines the main() method.

The startApp() method creates an object called a form and passes a string to the constructor that represents the form's title. A form is an instance of the class javax.microedition.lcdui.Form, which is a kind of screen that you can see on your display. It's so named because it functions somewhat like an HTML form—it contains one or more visual items, such as strings.

Next, the startApp() method creates a regular String object and adds it to the form. It then gets a reference to an object called a display, and it sets the form object as the currently displayed entity of the display.

After all this code executes, you see the screen. When you click or press the handset
button that tells the device to hang up, the AMS invokes destroyApp(), which simply eliminates all references to the form object previously created. It's now subject to garbage collection. The AMS then terminates the MIDlet.

You're responsible for properly disposing of objects created by your MIDlets. In this contrived case, it shouldn't matter whether or not you set the reference to the form variable to null, because the MIDlet terminates. But in general, you need to properly manage the references to your program's objects, just as you would in any Java program.




Comments(4)


1. Can u tall me about the job market of J2ME.

By: mimdad07 at 2009-06-26 14:40:26
2. thanks a lot it was really helpful...........for dummies like me

By: Joshi Rocky at 2010-01-29 14:13:40
3. This the very good introduction to the J2ME for the beginners like me.

By: chetan jain at 2010-04-01 06:39:32
4. can u tell me mobile tracker code in j2me
which send 2 sms to specific number whike changing the sim

By: aiyaz at 2010-08-21 16:29:08

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-07-30]Code sample to Send SMS from a J2ME application.
[2009-05-29]Adding your own Application icon for your J2ME application (jar file)
[2008-08-18]Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
[2008-08-01]Datagrams in J2ME (UDP Programming sample)
[2008-08-01]Client Server in J2ME (Socket Programming sample)
[2008-08-01]Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
[2008-08-01]Using HTTP vs UDP vs Socket in J2ME
[2008-08-01]RMSCookieConnector - Using Cookies in J2ME
[2008-07-29]POST UTF-8 encoded data to the server in J2ME
[2008-07-10]lists, forms, choices, gauges, text fields, text boxes in J2ME
[2008-07-10]Using List to create a Menu and Menu items in J2ME
[2008-07-10]Using alerts and tickers in J2ME
[2008-07-07]J2ME Canvas sample to show games programming in J2ME
[2008-07-07]Timer and TimerTask example in J2ME
[2008-06-27]List of GPRS Access points for all countries

More Latest News

Most Viewed Articles (in last 30 days)
RMS Basics in J2ME
Client Server in J2ME (Socket Programming sample)
Code sample to Send SMS from a J2ME application.
Getting Started with J2ME
GUI components and menu based J2ME Applications.
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
TextBox sample program in J2ME
J2ME Canvas sample to show games programming in J2ME
Timer and TimerTask example in J2ME
What is J2ME?
Adding your own Application icon for your J2ME application (jar file)
Download a file over a network in J2ME midlet
Using List to create a Menu and Menu items in J2ME
DateField sample program in J2ME
Most Emailed Articles (in last 30 days)
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Getting Started with J2ME
Y.S. Sun Green Building Research Center
Sample J2ME code that shows various functionality of RMS.
Sample Java program shows how to Read a file over a network using J2ME midlet
Types of configurations in J2ME
'LINK.EXE' is not recognized as an internal or ext
RMS Basics in J2ME
GUI components and menu based J2ME Applications.
The MIDP Networking Model in J2ME
Your first J2ME program and a midlet lifecycle explained.
What is J2ME?
paint() sample program to draw a line in J2ME