SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Java Tutorials For Selenium WebDriver - Static And Non Static Methods

Right now we are learning different components of methods in java software development language as described in THIS POST. You can read about use of different ACCESS MODIFIERS and RETURN TYPES of method If you are not aware about it. One another component of method Is It can be static or non static method. Static keyword with method Is describes that this method Is static and If method do not have static keyword then that method Is non static in java software development. 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 java software development language as described bellow.

Main Difference Between Static And Non Static Methods In Java
  • 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. VIEW THIS POST to learn about object In Java.
  • 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.
Now Let us look at one simple example of static and non static methods and variables of java software development. Bellow given example describes you how to access static and non static stuff Inside static and non static methods of same class or different class.

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.

20 comments:

  1. more more more please ........

    ReplyDelete
  2. Excellent Tutorial.. Thanks a lot for all your hardwork.You are truely an inspiration for all us

    ReplyDelete
  3. Static method Is associated with the class while non static method Is associated with an object.
    - can you give example for this??

    ReplyDelete
  4. what are the factors to consider whether to use static or non-static method? why not just use non-static method so there will be no restrictions?

    ReplyDelete
    Replies
    1. Static refers to common property of all objects. for example company name of employees.

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Excellent... very Nice and very usefull....

    ReplyDelete
  7. Using static variable of other class 2
    accessing non static variables outside te class 30000
    byke2 non static method : price 30000
    byke1 static method : wheels2
    Why byke1() ; is executing again ........last line : "byke1 static method : wheels2"

    ReplyDelete
  8. Using static variable of other class 2
    accessing non static variables outside te class 30000
    byke2 non static method : price 30000
    byke1 static method : wheels2
    why again byke1(); is executing ......last line ..."byke1 static method : wheels2" Plz explain

    ReplyDelete
  9. Thank you! Thank you! Thank you!

    ReplyDelete
  10. Great! Please post some advanced java topics.. waiting!!!!!!!!!!!!

    ReplyDelete
  11. nice stuff, spelling of "below" is incorrect everywhere.

    ReplyDelete
  12. Exact explanations within comments for each line of code..neatly explained..Hats off for your work.Please do continue...

    ReplyDelete
  13. Exact explanations within comments for each line of code..Neatly explained..Keep up the good work.Please do continue.Much appreciated..

    ReplyDelete