Tuesday, 30 August 2016

static block

1.       static block
static block is a set of instructions, which are recognized and initialized at the of loading respective class byte code to the memory.
static block are able to allow only static members of the current class, static blocks are not allowing non-static members of  the current class. If we want to access non-static mumber of the current class then, we have to create object for the respective class and we have use the generated reference variable . 
 static block are not allowing “this” keyword.

Ex code :-

class A
{
                int i=10;
                static int j=20;
                static
                {
                                System.out.println("SB-A");
                                System.out.println(j);
                                //System.out.println(this.j);--->Error
                                //System.out.println(i);---->Error
                                A a=new A();
                                System.out.println(a.i);
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                }
}

Output:-

SB-A
20
10

Q) Is it possible to display a line of text on command prompt without using main() method static variable and static method ?
Ans):-
Yes, it is possible to display a line of text on commend prompt without using main() method, static variable and static method, but, by using static block.
D:\java1130\Test.java
Ex code :-
class A
{
                static
                {
                                System.out.println("Sai Software Solution");
                                System.exit(0);
                }
}
Output:-
D:\java1130> javac Test.java
D:\java1130> java Test
Note:-
The above question and answers are valid up to JAVA^ version, they are invalid in JAVA7 version, because, in JAVA6 version JVM will load main class byte code to the memory without checking main() method availability, at the time of loading main class byte code JVM will execute static block and JVM will display the required message. In case of JAVA& version, first, JVM will check whether main() method is available or not, if main() method is available then only JVM will load main class byte code to the memory, if main () method is available then JVM will provide the following exception without loading main class byte code.
Error:-
Main method not found in class Test, please define main method as
public static void main(String[] args)

Q) Is it possible to display a line of text on command prompt without using main() method, static variable and static method combination and static block ?
Ans):-
Yes, it is possible to display a line of text on commend prompt, without using main() method, static variable, static method and static block, but by using static Anonymous Inner class of object class.  
D:\java1130\Test.java
Ex code :-
class Test
{
                static Object o=new Object();
                {

                                {
                                                System.out.println("Welcome to Sai Software Solutions");
                                                System.exit(0);
                                }
                };
}

Output:-
>>javac Test.java
>>java Test
>>Welcome To Sai Software Solutions
Note:-
The above question and Answers are valid up to JAVA6 And this JAVA7 version Invalid.


No comments:

Post a Comment