Static and Non-Static Methods
- Static keyword with method
It describes that this method is static and If method do not have
static keyword then that method is non static.
- Same rule is applied for variables too. Static means stable and non static means unstable in common words. There are several difference between static and not static methods in as described bellow.
Main Difference Between Static And
Non Static Methods
- We can call static methods directly while we can not call non static methods directly. You need to create and instantiate an object of class for calling non static methods.
- Non static stuff (methods, variables) can not be accessible inside static methods Means we can access only static stuff inside static methods.
- Opposite to It, Non static method do not have any such restrictions. We can access static and non static both kind of stuffs inside non static methods
- Static method is associated with the class while non static method is associated with an object.
Created Two different class as
bellow.
package Test_Package1;
public class static_nonstatic {
static int wheel = 2;
int price = 25000;
public static void main(String[] args)
{ //static method.
//Can access static methods directly
Inside static method.
byke1();
//Can access static variables
directly Inside static method.
System.out.println("Main static
method : wheels = "+wheel);
//Can not access non static variables
directly inside static method.
//System.out.println("Main
static method : wheels = "+price);
//Can not access non static methods
directly inside static method.
//byke2();
//Created object of class to access
non static stuff Inside static method.
static_nonstatic sn = new
static_nonstatic();
//Now We can access non static
methods of class Inside static methods using object reference.
sn.byke2();
//Now We can access non static
variables of class Inside static methods using object reference.
System.out.println("Main static
method : price = "+sn.price);
}
public static void byke1(){ //static
method.
//Can access static variables
directly Inside static method.
System.out.println("byke1 static
method : wheels = "+wheel);
//Can not access non static variables
directly inside static method.
//System.out.println(price);
}
public void byke2(){ //non static
method.
//Can access static variable directly
inside non static method.
System.out.println("byke2 Non
static method : wheels = "+wheel);
//Can access non static variable
directly inside non static method.
System.out.println("byke2 Non
static method : price = "+price);
//Can access static methods directly
Inside non static method.
byke1();
}
}
package Test_Package1;
public class static_ousideclass {
public static void main(String[] args)
{ //static method.
//Can call static function from other
class directly using class name.
static_nonstatic.byke1();
//Can call static variables from
other class directly using class name.
System.out.println("Using static
variable of other class"+static_nonstatic.wheel);
//Created object of class
static_nonstatic to access non static stuff from that class.
static_nonstatic oc = new
static_nonstatic();
//Now We can access non static
variables of other class Inside static methods using object
reference.
System.out.println("Accessing
non static variable outside the class : "+oc.price);
//Now We can access non static
methods of other class Inside static methods using object reference.
oc.byke2();
}
}
As you can see in above examples, we
can access only static stuff Inside any static methods directly. If
you wants to access static method or variable Inside different class
then you can access It using simply class name as shown In above
example. You must have to create object of class to access non static
method or variable Inside static method (byke1) of same class or
different class(main method of 2nd class).
On other side, we can access static and
non static methods and variables directly inside non static method
(byke2). There is not any such access restrictions.
No comments:
Post a Comment