Pages

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.

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.