INTRODUCTION:
Hey friends.. In this post I will explain you of what a
static method actually is.. and the main difference between static method and
instance method.
WHAT IS A STATIC METHOD..
I have already explained in detail of why the main ( )
method is static and other stuffs.. If you haven’t gone through it please refer
this why is main method static
When a method is declared as STATIC there is no need for
object creation..!!
If you know this already ..its fine.. but who doesn’t know
this yet.. their reaction currently will be … OH..REALLY ??
Yes.. we need not create any objects while calling that
method..
EXAMPLE:
Class classname
{
static void block( )
{
System.out.println(“static method is executed”);
}
public static void
main(String args[])
{
block( );
}
OUTPUT:
static method is
executed
EXPLANATION:
Usually static block cannot access normal variables and
methods directly. It can access only by creating objects.
Since the block( ) is a static method.. it can be accessed
from the main ( ) block which is a static method.
So, as you can see, block( ) method is been accessed
directly without any object creation done for it.
INSTANCE METHODS VS STATIC METHODS:
Consider the following example ..
Class classname
{
static void
block( )
{
System.out.println(“static method is executed”);
}
void NormalMethod( )
{
System.out.println(“normal method is executed”);
}
Public static void
main(String args[])
{
block( );
classname c = new classname( );
c.NormalMethod( );
}
OUTPUT:
static method is executed
normal method is executed
EXPLANATION:
In the above example.. the static block is accessed directly
and the Instance method(normal method) is accessed by creating the objects.
Object is created for the class and the method "NormalMethod"
is accessed and so the output.
We can’t access any NON- STATIC methods without object.
ACCESSING STATIC METHODS OUTSIDE THE CLASS:
Suppose if there is a situation where we want to access any
STATIC METHODS OUTSIDE THE CLASS then, it must be accessed by the class name
EXAMPLE:
Class classname
{
static void StaticMethod( )
{
System.out.println(“static method is executed”);
}
Class SecondClass
{
Public static void
main(String args[])
{
classname.StaticMethod
( );
}
}
OUTPUT:
static method is executed
EXPLANATION:
Here..
classname.StaticMethod ( ); must be written in order to call the static method which is present inside the class classname ..where, “classname” is the name of the class from where the
static method “StaticBlock” resides.
So..its as simple as that..
Just have 2 things in mind..
If a static method is inside the same class.. then no need
for object creation.. just call the method
While if the static method is in different class then call
to that method must be like “classname.static
method name “
CONCLUSION:
So.. that’s it for this post friends.. hit like button and
drop your feedback/ comments below.
Request / suggestions for the post you wish is also
welcomed..Email me or post as comments below..Share with your friends and help
me ,support me to post more..Have a plesant day :)
No comments :
Post a Comment