Static Execution Flow
Static Execution Flow
______________________________
static flow of execution
___________________________________________________
The Static Control Flow mechanism performs the following 3 steps in the exact
chronological order:
* Identification of static members from top to bottom. All the static variables,
methods,
and blocks are identified during this step.
* Execution of static variable assignments and static blocks from top to bottom.
* Finally, the static main method is executed in the last step of the static
control flow.
Que-1>
____________
What is static in Java?
---------------------------------------------------------
Que-2>
____________
How many static memebers are there in java ?
---------------------------------------------------------
Que-3>
_____________
Can we access static members if no instance of the class is constructed ?
--------------------------------------------------------
Que-4>
______________
What is the main use of static keyword in java?
-------------------------------------------------------
Que-5>
______________
Can we mark a local variable as static?
-------------------------------------------------------
Que-6>
_____________
public class A
{
private int x = 10;
static int m1() {
int y = x;
return y;
}
public static void main(String[] args) {
m1();
}
}
---------------------------------------------------------------------------
Que- 7 >
________________________
public class Demo04 {
static int a = 10;
static {
System.out.println("1st static block executed");
System.out.println(a);
}
static int b = m1();
static {
System.out.println("2nd static block");
System.out.println(a);
System.out.println(b);
}
static int c = 30;
}
-----------------------------------------------------------------------------
Que-8>
____________________
public class Demo04 {
static int a = m1();
static {
System.out.println("1st static block executed");
System.out.println(a);
}
static int b = 20;
static {
System.out.println("2nd static block");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
static int c = 30;
}
-----------------------------------------------------------------------
Que-9>
__________________
public class Demo04 {
static int a = 10;
static {
System.out.println("1st static block executed");
System.out.println(a);
}
static int b = 20;
static {
System.out.println("2nd static block");
System.out.println(a);
System.out.println(b);
}
static int c = m1();
}
--------------------------------------------------------------------------
Que-10>
___________________
public class Demo04 {
static int a = 10;
static {
System.out.println("1st static block executed");
System.out.println(a);
}
static int b = 20;
static {
System.out.println("2nd static block");
m1();
}
static int c = 30;
}
------------------------------------------------------------------------
Que-11>
_________________
public class Demo04 {
static int a = 10;
static {
System.out.println("1st static block executed");
System.out.println(a);
}
static int b = 20;
static {
System.out.println("2nd static block");
System.out.println(a);
b=m1();
}
static int c = 30;
}
--------------------------------------------------------------------
Que-12>
______________________
public class Demo04 {
static {
m1();
}
public static void main(String[] args) {
System.out.println(a);
}
static int a=10;
}
-------------------------------------------------------------------
Que-13>
______________
What will be the output of the following program?
----------------------------------------------------------------------------
Que-14>
_______________
What will be the output of the following code?
---------------------------------------------------------------------------
Que-16>
____________________
What is the output of the following program code?
--------------------------------------------------------------------------
Que-19>
______________________
public class StaticBlockExample {
// static block
static
{
// static data member
int num = 10;
System.out.println(num);
}
}
public static void main(String value) {
System.out.println(value);
}
}
------------------------------------------------------------------------
Que-21>
_______________________
class Demo04{
static int a=10;
static {
System.out.println("Hello");
}
public static void main(String[] value) {
System.out.println("From pre define main method");
String [] arr= {"sachin","mahi","chiku","Hitman"};
main(arr);
}
public static void main(String[] value1) {
for(String champions: value1) {
System.out.print(champions+" ");
}
}
}
-----------------------------------------------------------------------
Que-22>
_______________________
class Demo05 {
static int a = 10;// -----------------1 a= 10
static { // ------------------------------------2
m1();
System.out.println("Parent class static block");// 2nd ans
System.out.println("from parent static block " + a);
System.out.println();
}
a=30;
static {
System.out.println(a);
a=20;
a=70;
}
Ans: When a method is declared with the keyword ‘static’, it is called static
method in java.
Que-25>
____________________
Why is a static method also called a class method?
Ans: A static method is also called a class method because it ties to a class
rather than an individual instance of a class. Therefore, we need not to create an
object of the class to call and execute static method.
Que-26>
____________________
Can we access static members (such as static variables and static methods) from an
instance method?
Ans: Yes, we can access static members from an instance method in java.
Que-27
____________________
Is it possible to access instance members from a static method?
Ans: No, it is not possible to access instance members like instance variable and
instance method from a static method.
Que-28>
_____________________
Can we have a static method in an interface?
Ans: Yes, from Java 8 and onwards, the interface allows to define a static method
with body.
Que-29
_____________________
Can we use this or super keyword in static method in Java?
Ans: No, In the entire core java, this and super keywords are not allowed inside
the static region.
Que-30>
_____________________
Is it possible to overload static methods in a class?
Ans: Yes, we can overload static methods but override them. This is because they
are bound with class, not instance.
Que-31>
_____________________
Is it possible to override static methods of a class?
Ans: No, we cannot override static methods because static methods belong to a
class, not individual objects, and are resolved at compile time by java compiler.
Que-32>
_____________________
Can we override an instance method as static?
Ans: No.
Que-33>
_____________________
Why static block is executed before the main method in java?
Que-34>
_____________________
Why static block is executed before the main method in java?
Que-35>
_____________________
What is the use of static block in java?
we want to write that logic inside static block that is executed during the class
loading.
we want to change the default value of static variables.
we want to initialize static variable of the class.
Que-36>
_____________________
Can we call the static variable from the static block if the static
variable is not defined ( or the static variable is read only) ?
Ans: No we cannot execute the static variable from the static block
if the variable is not define before .
Que-37>
______________________
Can we call the static variable from the static method if the static
variable is not defined ( or the static variable is read only) ?
Ans: Yes we can ( complete the ans based on the class explaination and ans in
out own words ).