Java Tutorials For Selenium WebDriver - Static And Non Static Methods

Right at nowadays nosotros are learning dissimilar components of methods in coffee software evolution linguistic communication every bit described inward THIS POST. You tin read most purpose of dissimilar ACCESS MODIFIERS and RETURN TYPES of method If you lot are non aware most it. One approximately other gene of method Is It tin survive static or non static method. Static keyword with method Is describes that this method Is static as well as If method usage non convey static keyword as well as thence that method Is non static in java software development. Same dominion Is applied for variables too. Static agency stable as well as non static agency unstable inward mutual words. There are several difference betwixt static as well as non static methods In java software evolution linguistic communication every bit described bellow.

Main Difference Between Static And Non Static Methods In Java
  • We tin telephone telephone static methods withdraw spell nosotros tin non telephone telephone non static methods directly. You involve to usage as well as instantiate an object of course of pedagogy for calling non static methods. VIEW THIS POST to larn most object In Java.
  • Non static materials (methods, variables) tin non survive accessible Inside static methods Means nosotros tin access solely static materials Inside static methods. Opposite to It, Non static method usage non convey whatever such restrictions. We tin access static as well as non static both form of stuffs Inside non static methods
  • Static method Is associated amongst the course of pedagogy spell non static method Is associated amongst an object.
Now Let us await at 1 elementary event of static as well as non static methods as well as variables of coffee software development. Bellow given event describes you lot how to access static as well as non static materials Inside static as well as non static methods of same course of pedagogy or dissimilar class.

Created Two dissimilar course of pedagogy every bit bellow.
package Test_Package1;  world course of pedagogy static_nonstatic {    static int bicycle = 2;  int cost = 25000;  world static void main(String[] args) { //static method.   //Can access static methods withdraw Inside static method.   byke1();      //Can access static variables withdraw Inside static method.   System.out.println("Main static method : wheels = "+wheel);      //Can non access non static variables withdraw within static method.   //System.out.println("Main static method : wheels = "+price);      //Can non access non static methods withdraw within static method.   //byke2();       //Created object of course of pedagogy to access non static materials Inside static method.   static_nonstatic sn = novel static_nonstatic();       //Now We tin access non static methods of course of pedagogy Inside static methods using object reference.   sn.byke2();      //Now We tin access non static variables of course of pedagogy Inside static methods using object reference.   System.out.println("Main static method : cost = "+sn.price);    }    world static void byke1(){ //static method.   //Can access static variables withdraw Inside static method.   System.out.println("byke1 static method : wheels = "+wheel);      //Can non access non static variables withdraw within static method.   //System.out.println(price);     }    world void byke2(){ //non static method.   //Can access static variable withdraw within non static method.   System.out.println("byke2 Non static method : wheels = "+wheel);      //Can access non static variable withdraw within non static method.   System.out.println("byke2 Non static method : cost = "+price);       //Can access static methods withdraw Inside non static method.   byke1();  } }


package Test_Package1;  world course of pedagogy static_ousideclass {   world static void main(String[] args) { //static method.      //Can telephone telephone static component from other course of pedagogy withdraw using course of pedagogy name.   static_nonstatic.byke1();      //Can telephone telephone static variables from other course of pedagogy withdraw using course of pedagogy name.   System.out.println("Using static variable of other class"+static_nonstatic.wheel);      //Created object of course of pedagogy static_nonstatic to access non static materials from that class.   static_nonstatic oc = novel static_nonstatic();       //Now We tin access non static variables of other course of pedagogy Inside static methods using object reference.   System.out.println("Accessing non static variable exterior the course of pedagogy : "+oc.price);      //Now We tin access non static methods of other course of pedagogy Inside static methods using object reference.   oc.byke2();   }  }

As you lot tin meet inward in a higher house examples, nosotros tin access solely static materials Inside whatever static methods directly. If you lot wants to access static method or variable Inside dissimilar course of pedagogy as well as thence you lot tin access It using exactly course of pedagogy cite every bit shown In inward a higher house example. You must convey to usage object of course of pedagogy to access non static method or variable Inside static method (byke1) of same course of pedagogy or dissimilar class(main method of 2d class).

On other side, nosotros tin access static as well as non static methods as well as variables withdraw within non static method (byke2). There is non whatever such access restrictions.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post