Friday, October 16, 2015

Understanding mysterious static - 2

Oh! that was interesting, if you tried executing the program in the previous post. For a second may be it gave a food for thought to your mind. Why main is not executing first? Why am i getting output as:

In static block...InterestingStatic1
In main...InterestingStatic1

May be something is happening before main is executing. I must know it. Everything is mystery until understood. So let me take you to another interesting and mysterious scenario :

Here is what you do. Nothing much. Add InterestingStatic1Test class to the project you created yesterday. Let the class InterestingStatic1 be there in the project without any changes.

package teststatic;
public class InterestingStatic1Test {
        System.out.println("In static block...InterestingStatic1Test");
    public void printSomething(){
        System.out.println("Just printing ... Welcome to Mysteries of Java");
    public static void main(String[] args) {
        System.out.println("In main...InterestingStatic1Test");
        InterestingStatic1Test justObject = new InterestingStatic1Test();
        InterestingStatic1 newObject = new InterestingStatic1();

Yes, you are right. I am going to ask you to guess the output first and then run the program. Does it match your guess? Why the output is what it is.  wait... there is more to come ... See you...

©K.A.N. Nancy

Thursday, October 15, 2015

Understanding mysterious static - 1

package teststatic;
public class InterestingStatic1 {
        System.out.println("In static block...InterestingStatic1");
    public static void main(String[] args) {
        System.out.println("In main...InterestingStatic1");
What do you think should be the output. Normal understanding is main is executed first by JVM. Run this code. Do you find something unexpected?
More interesting code … tomorrow… let the mystery of static be resolved slowly, so that you enjoy…

©K.A.N. Nancy

Wednesday, October 14, 2015

static in Java - 1

When static block in java class is executed ?
     a) when the class is compiled
     b) when the first method in class is executed
     c) when the object is created, but before the constructor is called
     d) when the object is created and after the constructor is called
     e) when the class is loaded

©K.A.N. Nancy

Tuesday, October 13, 2015

Adding 2 numbers using Java

Let us have fun to add two numbers in variety of ways :

public class FunWithAdd {
   private int num1;
   private int num2;
  public FunWithAdd(int num1, int num2){
   public int getNum1(){
       return num1;
    public int getNum2(){
       return num2;
    public void setNum1(int num1){
    public void setNum2(int num2){
   int sum(){
       return num1+num2;
   int sum(int n1, int n2){
       return n1+n2;
   int sumWithItr (int n1, int n2){
      int s=n1;
      for(int i=1;i<=n2;i++){
      return s;
   int sumRecursive(int n1, int n2){
        if (n2 ==0){
            return n1;
        return sumRecursive(++n1,--n2);

Create a class to test variety of sum methods.

public class Add2Nums {
    public static void main(String[] args) {
        /* Summing Locals */
        int num1=25;
        int num2=12;
        System.out.println("sum : "+(num1+num2));
        /* Sum using Object of FunWithAdd*/
        FunWithAdd numsObject= new FunWithAdd(30,60);
        System.out.println("Adding the numbers in Object : "+numsObject.sum());
        System.out.println("Adding the numbers in Object by sending data from outside :"+numsObject.sum(19,20));
        System.out.println("sum using sumWithItr method : "  +numsObject.sumWithItr(numsObject.getNum1(),numsObject.getNum2()));
        System.out.println("sum using sumRecursive: "+numsObject.sumRecursive(numsObject.getNum1(),numsObject.getNum2()));

Output :
sum : 37
Adding the numbers in Object : 90
Adding the numbers in Object by sending data from outside :39
sum using sumWithItr method : 16
sum using sumRecursive: 45

Change the values and have fun… :)
©Nancy : created at 2:07 am IST

Java EE 6 Collocated Model

EJB 3.1 provides subset of EJB. This subset is known as EJBLite. EJBLite is available as a part of Java EE6 Web Container. You can create local EJBs with and without interface view. 

EJB 3.1 The benefit is when Web component and EJB component need to access the object for business processing, no marshaling is required. If an EJB makes changes in the object state , web component can view the changes. 

The local EJBs can be packaged in a WAR file. This is called as Collocated model of Java EE 6.