Java notes odds ends


Published on


1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java notes odds ends

  1. 1. Prof. Mukesh N Tekwani [Mobile 9869 488 356] JAVA : ODDS AND ENDS Finalizer Methods In Java, objects are dynamically allocated using the new keyword. When no references to an object exist, that object is assumed to be no longer needed and the memory occupied by the object can be reclaimed. Java does this de-allocation of memory automatically by a technique called garbage collection. When an object is destroyed, it my need to perform some action. For example, if an object is holding some resource then we might want to make sure that this resource is freed before an object is destroyed. Java permits the programmer to do this by a mechanism called finalization. By using the technique of finalization, we can define the actions that should occur when an object is just about to be reclaimed by the garbage collector. To add a finalizer to a class, we define the finalize() method. The Java runtime calls that method whenever it is about to recycle an object of that class. Inside the finalize method, we can specify the actions that must that must be performed before an object is destroyed. The general form of the finalize() method is: protected void finalize() { // finalization code stmts } The keyword protected prevents access to finalize() method outside its class. Every class inherits the finalize() method from java.lang.Object final keyword A variable can be declared as final. This prevents the contents of the variable from being changed in the program. Thus, a final variable must be initialized when it is declared. E.g., final int MAXMKS = 100; Consider the following program: class FinalTest { public static void main(String args[]) { Page 1 of 7
  2. 2. Prof. Mukesh N Tekwani [Mobile 9869 488 356] final int MAXMKS = 100; MAXMKS = 50; System.out.println("Max mks = " + MAXMKS ); } } After the statement final int MAXMKS = 100; we are trying to change the value of MAXMKS to 50. When this program is compiled, we get the error: “Cannot assign a value to a final variable.” Life Cycle of an Applet Every Java applet inherits a set of default behaviours from the Applet class. When an applet is loaded, it passes through a number of states. These states are: 1. Born or initialization state 2. Running state 3. Idle State 4. Destroyed state Begin (Load Applet) Born Initialization start() stop() Running Idle start() destroy() Destroyed Dead Exit from Browser Page 2 of 7
  3. 3. Prof. Mukesh N Tekwani [Mobile 9869 488 356] We now discuss the various sates of an applet: 1. Initialisation state: When an applet is first loaded, it enters into the initialization state. This is done by calling the init() method of the Applet class. We say that the applet is born. Initialization occurs only once in an applet’s life cycle, i.e., the init() method can be called only once. In the initialization state we may do the following tasks: • Create objects needed by the applet. • Set up initial values • Load images/fonts • Set up colors To override the default behaviour of an applet, we create the init() method: public void init() { statements } 2. Running State: When the system calls the start() method of the Applet class, the applet goes into the running state. This state occurs automatically after the init() state. The start() method can be called more than once. public void start() { statements } 3. Idle or Stopped State An applet goes into the idle mode when : (i) we leave the web page containing the currently running applet, and (ii) we call the stop() method. public void stop() { statements } 4. Dead State An applet is said to be dead when it is removed from memory. This takes place when we leave the browser. The destroy() method is called. This stage occurs only once in an applet’s life cycle. public void destroy () { statements } Page 3 of 7
  4. 4. Prof. Mukesh N Tekwani [Mobile 9869 488 356] 5. Display State An applet goes into the display state whenever it has to perform some output operations on the screen. This happens immediately after entering the running state. We use the paint() method to do this. public void paint(Graphics g) { statements } General Characteristics of an Applet 1. Execution of an applet does not begin at main(). In fact, an applet may not have a main() method. 2. All applets must import java.applet. 3. All applets must import java.awt. 4. All applets run in a window. 5. Applets are not executed by the console-based Java run-time interpreter. All applets are executed by an applet viewer or by a web browser such as Internet Explorer / FireFox. 6. Output to the applet’s window is not performed by System.out.println() but it is done with various AWT methods such as drawstring(). 7. Applets are event driven. An applet waits until an event occurs. Passing Parameters to an Applet Parameters can be supplied to an applet by using the <PARAM..> tag Each <PARAM..> tag has a name attribute and a value attribute. Inside the applet code, the applet can refer to that parameter by name to find its value. Consider the following example: //Applet demo – Passing Parameters import java.awt.*; import java.applet.*; public class MyApplet extends Applet { String msg; public void init() { msg = getParameter(“string”); //receive parameter value if(msg = null) Page 4 of 7
  5. 5. Prof. Mukesh N Tekwani [Mobile 9869 488 356] msg = “Java”; msg = “Hello “ + msg; setBackground(; setForeground(Color.white); } public void paint(Graphics g) { g.drawString(msg, 20, 80); } } The HTML file for this applet is as follows: (save this file as Param.html) <HTML> <HEAD> <TITLE> APPLET WITH PARAMETERS </TITLE> </HEAD> <BODY> <APPLET CODE = PARAM WIDTH = 400 HEIGHT = 200> <PARAM NAME = "string" VALUE = "Applet "> </APPLET> </BODY> </HTML> Now we can view the applet by running the Param.html file in a browser or by giving the following command at the DOS prompt: appletviewer Param.html Drawing Lines //Applet demo – Drawing lines import java.awt.*; import java.applet.*; public class Lines extends Applet { public void init() Page 5 of 7
  6. 6. Prof. Mukesh N Tekwani [Mobile 9869 488 356] { setBackground(; setForeground(Color.white); } public void paint(Graphics g) { for(int i = 0; i < 300; i+=10) g.drawLine(0, 0, i, 100); } } Drawing Rectangle: Use: g.drawRect(40, 40, 50, 50); The general syntax is: drawRect(int top, int left, int width, int height) Drawing Circle/Ellipse: Syntax: drawOval(int top, int left, int width, int height) The shape is drawn in a bounding rectangle whose upper-left corner is specified by top, left. To draw a circle, give width and height equal. An applet that draws a Square inside a Circle //Applet demo – Square in a circle import java.awt.*; import java.applet.*; public class SquareInCircle extends Applet { public void init() { setBackground(; setForeground(Color.white); } public void paint(Graphics g) { final int XC = 100, YC = 100; int radius, diameter, halfside, side; radius = (int)(Math.random() * YC); Page 6 of 7
  7. 7. Prof. Mukesh N Tekwani [Mobile 9869 488 356] halfside = (int)(Math.sqrt(Math.pow(radius,2)/2)+ .5); diameter = radius * 2; side = halfside * 2; g.drawOval(XC-radius, YC-radius, diameter, diameter); g.drawRect(XC - halfside, YC - halfside, side, side); } } Here is the HTML file called SquareinCircle.html <HTML> <HEAD> <TITLE> MY APPLET FOR DRAWING A SQUARE IN A CIRCLE </TITLE> </HEAD> <BODY> <APPLET CODE = SquareInCircle WIDTH = 400 HEIGHT = 200> </APPLET> </BODY> </HTML> Here halfside = sqrt(radius2 / 2). Page 7 of 7