iii) Java Basics for Selenium (static and non-static concept)
Static Variable:
Static variable in Java is a variable that belongs to the class and initialized only once at the start of the execution. It is a variable that belongs to the class and not to object(instance ).
Static variables occupy the memory when program execution starts.
Example.,
static int i;
Static Method:
Static method in Java is a method which belongs to the class and not to the object. A static method can access only static data.
A static method can call only other static methods and can not call a non-static method from it.
A static method can be accessed directly by the class name and doesn’t need any object
Example.,
public static void function_name( ){ }
Non Static Variable:
A non-static variable does ot have the keyword static before the name of the variable. Any variable of a class which is not static is called a non-static variable or an instance variable.
Non-static variable occupies the memory when necessary and after use releases the memory.
Example.,
int i;
Non-Static Method:
A non-static method does not have the keyword static before the name of the method. A non-static method belongs to an object of the class and you have to create an instance of the class to access it. Non-static methods can access any static method and any static variable without creating an instance of the class.
Example.,
public void function_name( ){ }
Example:
package staticNonStatic;
public class StaticNonStaticConcept {
String str="Automation Testing using Selenium"; //Non Static Global Variable
static int i=10; //Static Global Variable
// Static method
public static void main(String[] args) {
StaticNonStaticConcept sn = new StaticNonStaticConcept(); // Class Object
staticMethod(); // static method calls directly
sn.nonStaticMethod1(); // non-static method call by object name
System.out.println(i); // static variable calls directly
System.out.println(sn.str); // non-static variable calls by object name
}
// Static method
public static void staticMethod() {
int j=10; //Local Variable
System.out.println("This is Static Method");
}
// non-static method
public void nonStaticMethod1() {
System.out.println("This is Non Static Method 1");
nonStaticMethod2(); // non-static method calls directly
System.out.println(str); // non-static variable calls directly
System.out.println(i); // static variable calls directly
staticMethod(); // static method calls directly
}
// non-static method
public void nonStaticMethod2() {
System.out.println("This is Non Static Method 2");
}
}
Inheritance:
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
Example.,
In the example below, the Creta class and CretaSXModel class (subclass) inherits the attributes and methods from the Hyundai class (superclass):
Hyundai.java
package inheritance;
public class Hyundai {
static String companyName= "Hyundai";
String carName;
String modelName;
int price;
public void startCar() {
System.out.println("Start Car");
}
public void stopCar() {
System.out.println("Stop Car");
}
public static void wheelBalancing() {
System.out.println("wheelBalancing");
}
}
Creta.java
package inheritance;
public class Creta extends Hyundai{
public static void main(String[] args) {
Creta c = new Creta();
c.startCar();
c.stopCar();
wheelBalancing();
c.suvProperties();
musicSystem();
}
public void suvProperties() {
System.out.println("suvProperties");
}
public static void musicSystem() {
System.out.println("musicSystem");
}
}
CretaSXModel.java
package inheritance;
public class CretaSXModel extends Creta{
public static void main(String[] args) {
CretaSXModel c = new CretaSXModel();
c.autoFeature();
c.suvProperties();
c.startCar();
c.stopCar();
wheelBalancing();
musicSystem();
}
public void autoFeature() {
System.out.println("autoFeature");
}
}
Comments
Post a Comment