static context and instance context control flow in Java the complete explanation

Published: 19 April 2024
on channel: Learn Java
161
5

You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.

control flow of static and instance context in Java:-
=========================================================
1. static context:-
==================
static blocks and static variables are automatically recognized and executed at the time of loading the .class file to the JVM.

However, the static methods are executed only during the method call.

Please be informed that the .class file will be loaded inside the JVM only once. At the time of loading the .class file, the static variables and blocks will get recognized and executed automatically.

2. instance context:-
=====================
The instance variables and blocks are recognized and executed before executing the respective class constructor.

However, the instance methods are recognized and executed at the time of method call with the object reference.

Please be informed that the instance methods and blocks will be recognized and executed every time the respective class constructors are called.

Examples:-
=========
package xyz;
public class Test
{
static int a=method1();
static int method1()
{
System.out.println(" M1");
return 1000;
}
static{
System.out.println("S A 1");
}
static{
System.out.println("S A 2");
}
public static void main(String[] args)
{

}
}

Examples:-
========
package xyz;
class C{
static{
System.out.println("S B C");
}
}
class A extends C
{
static{
System.out.println("S B A");
}
}
public class Test extends A
{
static{
System.out.println("S B");
}
public static void main(String[] args)
{

}
}

Examples:-
=========
package xyz;
public class Test
{
static{
System.out.println("S B");//It will be recognized at the time of loading the .class //file to the JVM
}
{
System.out.println("I B");//Before the respective class constructor
}
Test()
{
System.out.println("T Con ");
}
public static void main(String[] args)
{
new Test();
new Test();
}
}


On this page of the site you can watch the video online static context and instance context control flow in Java the complete explanation with a duration of hours minute second in good quality, which was uploaded by the user Learn Java 19 April 2024, share the link with friends and acquaintances, this video has already been watched 161 times on youtube and it was liked by 5 viewers. Enjoy your viewing!