Variable Types- WebDriver
There are different three types of variables available In java as bellow.
1. Local Variables
Variables which are declared Inside methods or constructor are called as local variables for that method or constructor.
Variables declared Inside method or constructor are limited for that method or constructor only. You can not access to It outside that method or constructor.
You can not use access modifiers with local variables because they are limited to that method or constructor block only.
Need to initialise the value of local variables before using It because there Is not any default value assigned to local variables.
2. Instance Variables (Non Static)
Instance variables are declared on class level which is parallel to methods or constructors (outside the method or constructor block).
Instance variables are generally used with objects. So they are created and destroyed with object creation and destruction.
Instance(non static) variables are accessible directly by all the non static methods and constructors of that class.
If you wants to access Instance(non static) variables Inside static method, You needs to create object of that class.
Instance variables are always Initialised with Its default values based on Its data types.
You can access Instance variable directly (by Its name) Inside same class. If you wants to access It outside the class then you have to provide object reference with variable name.
Vast usage of Instance variables in java programs and selenium webdriver tests.
3. Class Variables (Static)
Same as Instance variables, Class variables are declared on class level (outside the method or constructor block). Only difference Is class variables are declared using static keyword.
Class variables are used In rare case like when It Is predefined that value of variable will never change. In that case we can use class variables.
Class variables are created on program start and destroyed on program end.
Class variables are visible to all methods and constructors of class. Class variables are visible to other class based on Its access modifiers. Generally they are public.
We can access class variables directly using Its name Inside same class. If you wants to access It outside the class then you need to use class name with variable.
Bellow given example will give you some Idea about all three types of variables. Created two class to show you how to access class variable inside other class. Both these class will show you the access levels of all three types of variables.
public class Collage1 {
//Class Variables - Collage name will be same for both departments so declared as class(static) variable.
public static String Collage_Name = "A1 Collage";
//Instance Variables
private String Department = "Computer Engineering";
private String name;
private double percentile;
public static void main(String[] args) {//Static Method
//Can access class variable directly If needed. i.e. Collage_Name
Collage1 student1 = new Collage1("Robert");
student1.setPercentage(67.32);
student1.print_details();
//Can access Instance variable using object reference If needed.
//Example : student1.name = "Robert";
Collage1 student2 = new Collage1("Alex");
student2.setPercentage(72.95);
student2.print_details();
}
public Collage1(String student_name){//Constructor
//Can access Instance variable directly Inside constructor.
name = student_name;
}
public void setPercentage(double perc){
//Can access Instance variable directly Inside non static method.
percentile = perc;
}
public void print_details(){
int Year = 2014; //Local Variable - Can not access It outside this method.
System.out.println("Resultg Of Year = "+Year);
System.out.println("Student's Collage Name = "+Collage_Name);
System.out.println("Student's Department = "+Department);
System.out.println("Student's Name = "+name);
System.out.println("Student's percentile = "+percentile+"%");
System.out.println("**********************");
}
Console output will looks like bellow.
Resultg Of Year = 2014
Student's Collage Name = A1 Collage
Student's Department = Computer Engineering
Student's Name = Robert
Student's percentile = 67.32%
**********************
Resultg Of Year = 2014
Student's Collage Name = A1 Collage
Student's Department = Computer Engineering
Student's Name = Alex
Student's percentile = 72.95%
**********************
public class Collage2 {
private String Department = "Mechanical Engineering";
private String name;
private double percentile;
public static void main(String[] args) {
Collage2 student1 = new Collage2("Smith");
student1.setPercentage(57.35);
student1.print_details();
}
public Collage2(String student_name){
name = student_name;
}
public void setPercentage(double perc){
percentile = perc;
}
public void print_details(){
int Year = 2014;
System.out.println("Result Of Year = "+Year);
//Can access other class's class variable using that class name.
System.out.println("Student's Collage Name = "+Collage1.Collage_Name);
System.out.println("Student's Department = "+Department);
System.out.println("Student's Name = "+name);
System.out.println("Student's percentile = "+percentile+"%");
System.out.println("**********************");
}
}
Console output will looks like bellow.
Result Of Year = 2014
Student's Collage Name = A1 Collage
Student's Department = Mechanical Engineering
Student's Name = Smith
Student's percentile = 57.35%
**********************
No comments:
Post a Comment