What is non static block in java

Answer Posted / sujay

An instance block in a java program can be said to be a
non-static block. The scope of a static block is same as the
scope of that class, It will be loaded into the memory when
the class is loaded into the memory and unloaded at the time
of class unloading. But the scope of an instance
block(non-static block) is same as the scope of the object
of that class, it will be loaded into the memory when every
an objected is created and destroyed when that object is
destroyed. So, static block is created only once at the time
of class loading and globally available to all the objects
of that class where as a separate copy of instance block is
created for each object.
class MyClass
{
static
{
System.out.println("I am static, available for all
the objects of this class!");
}

{
System.out.println("I am non-static, available as a
separate copy for each object that is created from this
class!");
}
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is append function?

560


Can I use % with real numbers?

554


Explain public static void main(string args[]) in java.

522


Why doesn't the java library use a randomized version of quicksort?

537


What is an object class?

551






What is meant by object?

568


Can we restart a dead thread in java?

544


What is linked hashmap and its features?

531


What is a static class in java?

548


What is regex in java?

523


Can we declare a static variable inside a method?

519


Why java applets are more useful for intranets as compared to internet?

573


What are packages in java?

553


Which access specifier can be used with class ?

526


What is type inference in java8?

571