QA's approach to Java - Understanding Non static context
Non-Static Members Flow
Java supports 4 non-static members inside a
class
1) non-static block
2) non-static variable
3) non-static method
4) constructor
Note : to access these members, object
creation is required.
Ques : Where we can create object for a
class ?
Ans : using any one of 4 static members.
Generally to access any restricted
area(non-static), we need to take permissions in common area(static).
for example, to enter into
theater(non-static), we need to get ticket in box-office(static)
Example: Object created in Static main
method
class Demo
{
int
a ; //non-static variable, must be
intialized using constructor
Demo(
)
{
System.out.println("Constructor....");
}
{
System.out.println("non-static
block....");
}
public
static void main(String[] args)
{
System.out.println("main
method....");
new
Demo( ) ; //object creation....
}
}
Non-static variable :
1)
Declaration of variable inside the class and outside to all the methods and
blocks.
2)
non-static variable, must be initialized
using constructor.
3)
non-static variable only gets registration in method area while class is
loading....
4)
it gets memory allocation, inside the heap area when an object is created and implicitly initializes with default values.
Constructor :
1) It is a special java method.
2) It is defined with class name.
3) return type not allowed.
4) used to initialize object (will see
later).
5) we must call explicitly in the process
of object creation.
Non-static block :
1) Block of instructions having no identity.
2) we cannot call explicitly in java application.
3) JVM invokes implicitly in the process of object creation.
Note : static block executes only once in the application at the time of class loading.
But non-static block and constructor executes every time in the process of object creation.
Example 1:
class Demo
{
static
{
System.out.println("static block....");
}
Demo( )
{
System.out.println("Constructor....");
}
{
System.out.println("non-static block....");
}
public static void main(String[] args)
{
for(int i=1 ; i<=5 ; i++)
{
new Demo();
}
}
}
Note : static block and non-static blocks are optional in java application.
Example 2:
class Demo
{
Demo( )
{
System.out.println("Constructor....");
}
public static void main(String[] args)
{
new Demo();
}
}
static vs Non-static contexts:
Static members belong to class.
Non-static members belong to object.
Static members can be called using class name
Non-static members can be called using object reference.
Static block executes implicitly while class is loading.
Non-static block executes implicitly while object is creating.
Static variable get memory allocation and initializes with default values while class loading.
Non-static variable get memory allocation and initializes with default values while object is creating…..
Example 3:
class Demo
{
/*
static
void fun( )
{
System.out.println("static
user method.....");
}
*/
public
static void main(String[] args)
{
Demo.fun(
) ; //CE : fun() method not found.....
}
}
Note : While compiling java source program,
compiler is looking for constructor definition.
If not present, compiler will write one
zero arguments constructor with empty definition is called “Default constructor”
Example 4:
class Demo
{
/*
Demo(
)
{
System.out.println("Constructor.....");
}
*/
public
static void main(String[] args)
{
new
Demo(); //No error, invokes default constructor....
}
}
Comments
Post a Comment