Your first J2ME program and a midlet lifecycle explained.

By: Grenfel Emailed: 1697 times Printed: 2200 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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()
public void destroyApp(boolean destroy)
form = null;
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!";
// This app simply displays the single form created
// above.
display = Display.getDisplay(this);

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.

J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Can u tall me about the job market of J2ME.

View Tutorial          By: mimdad07 at 2009-06-26 14:40:26
2. View Comment

thanks a lot it was really helpful...........for dummies like me

View Tutorial          By: Joshi Rocky at 2010-01-29 14:13:40
3. View Comment

This the very good introduction to the J2ME for the beginners like me.

View Tutorial          By: chetan jain at 2010-04-01 06:39:32
4. View Comment

can u tell me mobile tracker code in j2me
which send 2 sms to specific number whike changing the sim

View Tutorial          By: aiyaz at 2010-08-21 16:29:08
5. View Comment

im beginning to design a program that will deactivate a bank account using a GSM message in case of loss of atm me what midlet should i use.or what is needed.thanks

View Tutorial          By: andrew at 2012-01-29 14:32:39
6. View Comment

Very nice introduction to j2me well you should also give a link to download which Development Kit.
Can we compile these codes in blueJ ?
Ive not totally understood the whole illustration but ya it was good next time please explain more familiar way.

View Tutorial          By: Bikashjit at 2012-08-29 08:53:28
7. View Comment

Good and optimum introduction to J2ME application life cycle as well as Coding Guidelines for my first J2ME application.

View Tutorial          By: at 2013-01-14 10:37:57
8. View Comment

Hi, this was useful. how can integrate j2me program to get the text information from the other different file and display in the Sun java WIreless toolkit.

Please send me the code to mail id:

View Tutorial          By: karthik at 2013-01-23 10:02:02

Your name (required):

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

Your sites URL (optional):

Your comments:

More Tutorials by Grenfel
Verifying a Signed JAR File example
Bitwise and Bit Shift Operators example in Java
JavaBeans Basic Concepts
The Differences Between Simple and Classic Tags in JSP
Bitwise Logical Operators in C
DispatchAction in Struts
union example program in C
Comparison operators in JSP
Basics of Structures in C
register Variables in C
Struts and Tiles - Steps to use Struts and Tiles
The if else statement in C
Character Counting sample program in C
Installing JSF
cin.ignore() in C++

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

More Latest News
Most Viewed Articles (in J2ME )
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Y.S. Sun Green Building Research Center
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
DateField sample program in J2ME
List of GPRS Access points for all countries
Download a file over a network in J2ME midlet
Sample J2ME code that shows various functionality of RMS.
GUI components and menu based J2ME Applications.
Your first J2ME program and a midlet lifecycle explained.
Ticker sample program in J2ME
RMS Basics in J2ME
Timer and TimerTask example in J2ME
POST UTF-8 encoded data to the server in J2ME
Most Emailed Articles (in J2ME)
What is J2ME?
Download a file over a network in J2ME midlet
TextBox sample program in J2ME
Sample program to draw a arc in J2ME
Client Server in J2ME (Socket Programming sample)
'LINK.EXE' is not recognized as an internal or ext
Image and ImageItem sample program in J2ME
Sample program to draw a rectangle in J2ME
RMS Basics in J2ME
Types of configurations in J2ME
Types of Profiles in J2ME
A sample J2ME Auto-resume program.
lists, forms, choices, gauges, text fields, text boxes in J2ME
Using alerts and tickers in J2ME
Datagrams in J2ME (UDP Programming sample)