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

Veröffentlicht am: 19 April 2024
auf dem Kanal: 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();
}
}


Auf dieser Seite können Sie das Online-Video static context and instance context control flow in Java the complete explanation mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Learn Java 19 April 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 161 Mal angesehen und es wurde von 5 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!