Skip to main content

Difference between Static and non-static method in Java

In case of non-static method memory is allocated multiple time whenever method is calling. But memory for static method is allocated only once at the time of class loading. Method of a class can be declared in two different ways
  • Non-static methods
  • Static methods

Difference between non-static and static Method

Non-Static methodStatic method
1These method never be preceded by static keyword
Example:
void fun1()
{
 ......
 ......
}
These method always preceded by static keyword
Example:
static void  fun2()
{
......
......
}
2Memory is allocated multiple time whenever method is calling.Memory is allocated only once at the time of class loading.
3It is specific to an object so that these are also known as instance method.These are common to every object so that it is also known as member method or class method.
4These methods always access with object reference
Syntax:
Objref.methodname();
These property always access with class reference
Syntax:
className.methodname();
5If any method wants to be execute multiple time that can be declare as non static.If any method wants to be execute only once in the program that can be declare as static .
Note: In some cases static methods not only can access with class reference but also can access with object reference.
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
}
}

Output

Hello I am Non-Static
Hello I am Static
Following table represent how the static and non-static properties are accessed in the different static or non-static method of same class or other class.
Static and non-Static Method

Program to accessing static and non-static properties.

Example

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();
}
}

Comments

Popular posts from this blog

Introduction To C++

       Lets learn The C++ Programming Language C++ ranks 4th in popularity according to 2016 IEEE spectrum Top Programming Language ranking. Learning C++ is a wise investment for all programmers.                    What is C++? “C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.” In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Srroutrup in 1979. Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today. Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.                             ...

C++ Loops (For, While and Do while)

                                                   The for statement The C++ for statement has the following form: Syntax: for  (expression1;Condition;expression2) statement; for  (expression1;Condition;expression2) { block of statements } expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment); NOTE: C/C++ basically treats for statements as while type loops For loop example program: /* Example Program For for Loop In C++ Programming Language */ // Header Files #include<iostream> using namespace std; //Main Function int main() { // Variable Declaration int x=3; //for loop for (x=3;x>0;x--) { cout<<"x="<<x<<endl; } //Main Function return Statement return 0; } Output: x=3 x=2 x=1 ...

Conditional structure: if and else

                                                 Conditional structure: if and else The if statement executes based test expression inside the braces. If statement expression is to true, If body statements are executed and Else body statements are skipped. If statement expression is to false If body statements are skipped and Else body statements are executed. Simply, Block will execute based on If the condition is true or not. IF conditional statement is a feature of this programming language which performs different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch prediction, this is always achieved by selectively altering the control flow based on some condition. if and else Syntax if (expression) // Body will execute if expression is true or non-zero { //...