The above question means that we need to execute some line of code which perform something before the main() method execution. As we know that when we execute our program JVM will call main() at very first , so it seems that its not possible to perform something before the main() method execution but is possible with the static initialization block and calling method while initializing static variable 

Important Points :

1. Static initialized will always be executed before main() method calling by JVM.
2. Final static variable can be initialized using static initialization block.

1st Approach by static initialization block: 

Program :

public class Lab1 {

    static {                       // static initialization block
 int sum=10+20;
 System.out.println("SUM:"+sum);
    }

    public static void main(String[] args) { // main method
 
       System.out.println("main() method");
    }

}

In the above program sum=10+20 will be executed before main() method and hence output will look like ,
SUM:30
main() method

NOTE : We can also call a static method from static initialization block which too can perform some task which will always counted before main() method execution.


public class Lab1 {

    static {                 // static initialization block
 int s=Lab1.sum(10,20);
        System.out.println("SUM:"+s);
    }

    public static int sum(int a,int b ){
        return (a+b);
    }

    public static void main(String[] args) { // main method
 
       System.out.println("main() method");
    }

}


Now in the above program static sum() method will be called first before main() method. And the output will look like ,

SUM:30
main() method

2nd Approach by Calling static method while initializing static variable :

public class Lab1 {

    static int x=Lab1.sum(10,20); 

    public static int sum(int a,int b ){
        System.out.println("SUM:"+(a+b));
        return (a+b);
    }

    public static void main(String[] args) { // main method
 
       System.out.println("main() method");
    }

}



In the above program at the time of call loading JVM will try to initialize the static variable 'x' here and while initializing it will call static method sum(). So this method will be executed before the main() method execution. The output will look like,

SUM:30
main() method







Related Posts:

  • Local inner class in Java Local inner class is a class which is define local to the outer class. So it must be defined within the local scope of a class like method, constructor or initialization block . A class within a class in never treated as the… Read More
  • Inner Class in Java Inner class in a class defined as a class inside another class. So it is itself a class but first it will act as a member of that class. As we know that we cannot make a class as static but inner class can be made as static… Read More
  • Anonymous class in Java Like other classes in java , anonymous is also a type of class but it doesn't have any name like other classes have their own name either defined by the user or pre-defined in the java by the java vendors. So anonymous class… Read More
  • Instance Inner class in Java As the name itself indicates that inner class with non-static scope that means a member of outer class which is of instance scope called as instance inner class. Instance inner class is itself a class but we cannot use any s… Read More
  • Static inner class in Java Like Instance inner class we have also static inner class. Static instance class is loaded at the time of loading the outer class as it is declared as static that means it will act as the static member of the outer class and… Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic