You are on page 1of 6

Difference between non-static and static Method

Non-Static method Static method

These method never be preceded by


These method always preceded by static keyword
static keyword
Example:
Example:
static void fun2()
void fun1()
1 {
{
......
......
......
......
}
}

Memory is allocated multiple time Memory is allocated only once at the time of class
2
whenever method is calling. loading.

It is specific to an object so that these are These are common to every object so that it is
3
also known as instance method. also known as member method or class method.

These methods always access with object


These property always access with class reference
reference
4 Syntax:
Syntax:
className.methodname();
Objref.methodname();

If any method wants to be execute


If any method wants to be execute only once in
5 multiple time that can be declare as non
the program that can be declare as static .
static.

Note: In some cases static methods not only can access with class reference but also can access with
object reference.

Example

class Counter

int count=0;//will get memory when instance is created


Counter()

count++;

System.out.println(count);

public static void main(String args[])

Counter c1=new Counter();

Counter c2=new Counter();

Counter c3=new Counter();

Output

Program of counter by static variable

As we have mentioned above, static variable will get the memory only once, if any object changes the
value of the static variable, it will retain its value.

Example

class Counter

static int count=0; //will get memory only once

Counter()

count++;

System.out.println(count);

public static void main(String args[])


{

Counter c1=new Counter();

Counter c2=new Counter();

Counter c3=new Counter();

Output

Example of Static and non-Static Method

Example

class A

void fun1()

System.out.println("Hello I am Non-Static");

static void fun2()

System.out.println("Hello I am Static");

class Person

public static void main(String args[])

{
A oa=new A();

oa.fun1(); // non static method

A.fun2(); // static method

class A

int y;

void f2()

System.out.println("Hello f2()");

class B

int z;

void f3()

System.out.println("Hello f3()");

A a1=new A();

a1.f2();

class Sdemo

static int x;

static void f1()


{

System.out.println("Hello f1()");

public static void main(String[] args)

x=10;

System.out.println("x="+x);

f1();

System.out.println("Hello main");

B b1=new B();

b1.f3();

Static block:

More than one static block in a program

class StaticDemo

static

System.out.println("First static block");

static

System.out.println("Second Static block");

}
public static void main(String args[])

System.out.println("This is main()");

Output:

First static block

Second static block

This is main()

Note: "Here static block run according to there order (sequence by) from top to bottom.

Why a static block executes before the main method ?

A class has to be loaded in main memory before we start using it. Static block is executed during

class loading. This is the reason why a static block executes before the main method.

You might also like