Wednesday, June 15, 2011

static block

Static block resembles a method with no name/arguments/return type. There is no need to refer to it from outside the class definition, therefore it doesn’t need a name. If it doesn’t have any name, who is executing this block? Simple :) This block is executed by the Java VM when the class is loaded.

Why Do I Need a Static Block

Static blocks are very useful if you consider doing things like below:

  • If you’re loading drivers and other items into the namespace. (for instanceClass.forName(”org.h2.Driver”))
  • Initialize your static members at once (most likely this would be singletons, etc)
  • Security related issues or logging related tasks
  • Some database operations like creating prepared SQL statements, etc
Execution Order

Java doesn’t limit you with one static block. So you may define more than one static block. JVM combines them together and then executes. Here you might think what about the order of all these statements? Since static code blocks could be separated by non-static code blocks, the execution might not be in the same order you see in the code. You must remember that JVM combines all these blocks into one single static block and then executes. Here are some notes from my understanding:

  • If you’re defining static variables/methods, JVM will execute these statements when they need.
  • If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM.
  • If you’re referring some static variables/methods from the static blocks, these statements will be executed after the class is loaded into JVM (same time as second).


   1:  package certify;
   3:  public class StaticBlock {
   5:      public static final String name = "Static Variable Name";
   7:      {
   8:          System.out.println("Instance Initialization Block");
   9:      }
  10:      static {
  11:          System.out.println("Static Block 1");
  12:          show();
  13:      }
  14:      public StaticBlock() {
  15:          System.out.println("CONSTRUCTOR");
  16:      }
  17:      public static void show() {
  18:          System.out.println("Static Method");
  19:      }
  20:      public static void main(String[] args) {
  21:          System.out.println("MAIN");
  22:          new StaticBlock();
  23:          new StaticBlock();
  24:      }
  25:      static {
  26:          System.out.println("STATIC BLOCK 2");
  27:          System.out.println("Static Block Name = " + name);
  28:      }
  29:  }


Static Block 1
Static Method
Static Block Name = Static Variable Name
Instance Initialization Block
Instance Initialization Block

So what happened in above code? Let me try to explain. First of all, Java didn’t do anything before the class is loaded into JVM. After loading, JVM combined all the static blocks into one regardless of their position in the code, and started to execute each line. If line is executable JVM executed that line, otherwise it just kept the method/variable blocks on a special area in the memory. And when they are needed, JVM executed those statements too. If you double check, we have 2 static methods, one is show and the other is main. Show is executed as the first item since it is called within the static block. However, main is executed after all static initializations.

One thing you might notice is there is another block without static keyword. And it’s been executed twice. Yes, that block is not a static block. It is instance initialization block.


Yes, we have couple limitations about static blocks:

  • You shouldn’t try to access super since there is no such a thing for static blocks
  • You cannot use this keyword since there is no instance :)
  • You cannot throw Checked Exceptions
  • And please don’t try to return anything from this block

Sunday, June 5, 2011

Java Network Launching Protocol (JNLP)

Programmers often speak of the Java Network Launching Protocol (JNLP) interchangeably with the term "Web Start". The JNLP protocol, defined with an XML schema, specifies how to launch Java Web Start applications. JNLP consists of a set of rules defining how exactly to implement the launching mechanism. JNLP files include information such as the location of the jar package file and the name of the main class for the application, in addition to any other parameters for the program. A properly configured browser passes JNLP files to a Java Runtime Environment (JRE) which in turn downloads the application onto the user's machine and starts executing it. The development of JNLP took place under the Java Community Process as JSR 56. It includes the original 1.0 release, the subsequent 1.5 maintenance release, and as of 2006[update], the pending 6.0 maintenance release. JNLP comes free of charge; developers need not pay a license fee in order to use it in programs.

Important Web Start features include the ability to automatically download and install a JRE in the case where the user does not have Java installed, and for programmers to specify which JRE version a given program needs in order to execute. The user does not have to remain connected to the Internet to execute the downloaded programs, because they execute from a locally-maintained cache. Updates of the software download from the Web become available when the user has a connection to the Internet, thus easing the burden of deployment.

Any computer user can use JNLP by simply installing a JNLP client (most commonly Java Web Start). The installation can occur automatically such that the end user sees the client launcher downloading and installing the Java application when first executed.

JNLP works in a similar fashion to how HTTP/HTML works for the web. For rendering a HTML webpage, after the user clicks on a weblink, the browser submits a URL to a webserver, which replies with an HTML file. The browser then requests the resources referred to by this file (images, css), and finally renders the page once it has received enough information. Page rendering usually starts before all resources have downloaded; some resources not critical to the layout of the page (such as images), can follow on afterwards — or on request if the "Load Images Automatically" browser-setting remains unset.

JNLP mirrors this process; in the same way that a Web browser renders a webpage, a JNLP client "renders" a Java app. After the user clicks on a weblink the browser submits a URL to a webserver, which replies with a JNLP file (instead of a HTML file) for the application. The JNLP client parses this file, requests the resources specified (jar files), waits for the retrieval of all required resources, and then launches the application. The JNLP file can list resources as "lazy", which informs the JNLP client that the application does not need those resources to start, but can retrieve them later on when/if the application requests them.