Core Java Programming Challenges #6 | Coding Challenges | Naresh IT

preview_player
Показать описание
Core Java Programming Challenges #6 | Coding Challenges | Naresh IT

💡 Also Watch

Java Programming Tutorials by Mr.Hari krishna:
Advanced Java Programming Tutorials by Mr.Nataraj:

Subscribe to our channel and hit the bell 🔔🔔🔔 icon to get video updates.

💡 Visit Our Websites
#CoreJava_Programing #Challenges #CoreJava #Quiz
--------------------------

💡 About NareshIT:

"Naresh IT is having 14+ years of experience in software training industry and the best Software Training Institute for online training, classroom training, weekend training, corporate training of Hadoop, Salesforce, AWS, DevOps, Spark, Data Science, Python, Tableau, RPA , Java, C#.NET, ASP.NET, Oracle, Testing Tools, Silver light, Linq, SQL Server, Selenium, Android, iPhone, C Language, C++, PHP and Digital Marketing in USA, Hyderabad, Chennai and Vijayawada, Bangalore India which provides online training across all the locations

--------------------------

💡 Our Online Training Features:
🎈 Training with Real-Time Experts
🎈 Industry Specific Scenario’s
🎈 Flexible Timings
🎈 Soft Copy of Material
🎈 Share Videos of each and every session.

--------------------------

💡 Please write back to us at

--------------------------

💡 Check The Below Links

Рекомендации по теме
Комментарии
Автор

static blocks will be executed at the time of class loading. Only one time for the perticular class. Whereas instance blocks are specific for every object. It will be executed for each and every object creation for the perticular class.

At the time of Object creation these following operations are step by step performed:

1.) Identification of instance memeber varaibles and instance blocks.
2.) Execution of instance varaible assignments and execution of instance blocks from top to bottom.
3.) Execution of constructor for that object.
4. After that execution of other statements written below the object creation statement.

In the case of Parent and Child class steps are as usual same but executed from parent to child class.

deepakpatre
Автор

instance blocks will be executed based on number of object creations but static blocks executes only once irrespective of object creation

sravyarani
Автор

1) Static block will execute only once in that particular Class life.

2) Instance Initializer Block (Non-Static) block will execute for every object that is created for that Class. If we create 3 Object of an class Then Instance Initializer Block also execute 3 times.


Code Snippet :-

Class Foo {

static {
System.out.println("I am Static Block"):
}//static block

{
System.out.println("I am Instance Initializer Block");
}//IIB


public static void main(String[] args) {
System.out.println("I am Main Method");

Foo foo = new Foo();
Foo foo1 = new Foo();
}//Main

}//class

Output :-
I am Static Block
I am Instance Initializer Block
I am Instance Initializer Block

apx
Автор

Static block will execute only one time in one class creation and non static block depends on the how many no of time Object has created.

deepakvishwakarma
Автор

public class Exercise6 {

static int x;

static{
x = 10;
System.out.println( " x first: " + x);
}

public static void main(String[] args) {
System.out.println( " x : " + x);
}
}

isurid.keshani
Автор

class main{
static{
System.out.println("hi, i'm from static block!");
}
{
System.out.println("hi, i'm from instance block");
}

public static void main(String[] args){
main x = new main();
System.out.println("hi. i'm from main method!");
main y = new main(); // instance block is calling at the time of bject calling
main z = new main();
}
}

dhruvaarora
Автор

static-1
and instance and constructors execution depends on how many no of object created.

praveenautomationinterview
Автор

//static block will execute only once
//But non Static Blocks are executed based on how many times we created the object, that many times
//non-static blocks are created

public class Blocks {
static
{
int a=20;
System.out.println(a);
}
static
{
int b=10;
System.out.println(b);
}
public static void main(String[] args) {
Blocks b= new Blocks();
Blocks b1= new Blocks();
Blocks b2= new Blocks();
}

}
output:
20
10
10
10

shilpar
Автор

public class Cge6 {
static {
System.out.println("This is a static method");
}

{
System.out.println("This is a instance method");
}
public static void main(String[] args) {

Cge6 A = new Cge6();
// Number of objects created for class = number of times the instance block exectes
}
}

kranjithkumar
Автор

public class Main
{
public static void main(String[] args)
{
Test a = new Test();
Test b = new Test();
}

static class Test{
static{
System.out.println("Static block executed");
}

{
block executed");
}
}
}

Output :
Static block executed
Instance block executed
Instance block executed

so it proves that static block executes at one time of class loading and instance block execution depends upon object creation as how many time it is created.

mdtausifiqbal
Автор

public class Index {
static{
System.out.println("Static block"); // this block will be executed only once at the time of class loading``
}

{
System.out.println("non static block"); //the execution of this block is equal to the number of objects created
}
public static void main(String[]args)
{

Index i1 = new Index();
Index i2 = new Index();


}

}

nishantraj
Автор

public class PC6
{
static int count=0;
static int scount=0;

//Static Block
static{
scount++;
System.out.println("Static Block Executed : " + scount + " Time");
}

//instance Block(Non-static Block)
{
count++;
System.out.println("Instance Block Executed : " + count +" Time");
}
public static void main(String[] args) {

PC6 obj1=new PC6();
PC6 obj2=new PC6();
PC6 obj3=new PC6();

}
}

kundansahu
Автор

public class instanceBlock {
{
System.out.println("This is instanceBlock : 1");
}

public static void main(String [] args){

instanceBlock ins = new instanceBlock();
}
}

TheHauntedBharat
Автор

class Test
{
static
{
System.out.println("This is ststic block");
}
{
System.out.println("This is non static block");
}
}
public class StaticBlock {

public static void main(String[] args) {
Test t=new Test();
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
}

}

ganeshkoli
Автор

public class P6 {

static int cnt1 = 0;
static int cnt2 = 0;

static
{
cnt1++;
}
{
cnt2++;
}
public static void main(String[] args) {

P6 obj1 = new P6();
P6 obj2 = new P6();
P6 obj3 = new P6();

System.out.println("Static block executed "+cnt1+ " times");
System.out.println("Instance block executed "+cnt2+ " times");

}
}

vishaljadhav
Автор

class Challenge6 {

static int count1;
static int count2;

static{
count1++;
System.out.println("Static block is executed "+count1+" time");
}

{
count2++;
System.out.println("Non static block is executed "+count2+" time");
}

public static void main(String[] args) {
Challenge6 c1=new Challenge6();
Challenge6 c2=new Challenge6();
Challenge6 c3=new Challenge6();
Challenge6 c4=new Challenge6();
}
}

Yogesh-ldjs
Автор

class sample {
static {

System.out.println("static ");
}
{

}

}

class Main {
public static void main(String args[]) {

sample s1=new sample();
sample s2=new sample();
}
}

sravyarani
Автор

class Practice {

static
{
System.out.println("this is static block");
}

{
System.out.println("this is instance block");
}
public static void main(String[] args) {

Practice practice1 = new Practice();

Practice practice2 = new Practice();
}

}

NageswaraRao-el
Автор

public class Interviewprac {
static{
System.out.println("this is static block");
}
{
System.out.println("This is non static block");
}

public static void main(String[] args) {
Interviewprac a1 = new Interviewprac();
Interviewprac a2 = new Interviewprac();
Interviewprac a3 = new Interviewprac();
}
}
output:
this is static block
This is non static block
This is non static block
This is non static block

static block execute only once in class during class loading but non static depends on the number of time object has been created of class.

deepakkumarvishwakarma
Автор

Static block execution : only once i.e at the time of class loading
Instance or Non Static Block execution : As no. of times we create object for th class
My Code :


public class StaticAndInstanceBlockExc {

static {

System.out.println("Static Block executes");
}

{
Block executes");
}

public static void main(String args[]) {
StaticAndInstanceBlockExc obj1 = new StaticAndInstanceBlockExc();
StaticAndInstanceBlockExc obj2 = new StaticAndInstanceBlockExc();
}

}
output:


Static Block executes
Instance Block executes
Instance Block executes

jaisreeramprasad
welcome to shbcf.ru