Variable Types In Java - WebDriver Tutorials With Java

Before learning about variable types In java software programming language, I recommend you to read my posts related to variable's  DATA TYPESACCESS MODIFIERS and STATIC AND NON STATIC VARIABLES. As you know, Variable provide us a memory space (based on type and size of declared variable) to store a value. There are different three types of variables available In java software development language as bellow.

1. Local Variables
  • Variables which are declared Inside methods or constructor are called as local variables for that method or constructor in java software programming language. 
  • 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 In java software development language.
  • Need to initialize 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 In java software development language.
  • 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 Initialized 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 software programs and selenium webdriver software automation 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("Resultg 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.
Resultg Of Year = 2014
Student's Collage Name = A1 Collage
Student's Department = Mechanical Engineering
Student's Name = Smith
Student's percentile = 57.35%
**********************

4 comments:

  1. Can we access non-static varibles in a static method?

    ReplyDelete
    Replies
    1. No, you can't access non-static members inside static methods. You can call only static members inside static methods.

      In case, where you need to access non-static members, just create an object (instance of the class to which the non-static members belongs) and call them.

      public class Acce {
      public static int x = 10; //static variable
      public int y = 20; //Non-static variable

      public void Test1(){ //Non-static Method
      System.out.println("Test3 Completed");
      }

      public static void Test2(){ //Static Method
      System.out.println("Test3 Completed");
      }

      public static void main(String[] args){
      Test2(); //calling static method
      //or
      Acce.Test2(); //calling static method

      System.out.println(x); //calling static variable
      //or
      System.out.println(Acce.x); //calling static variable

      //To call Non-static members
      Acce dummy = new Acce(); //create object

      dummy.Test1(); //calling Non-static method
      System.out.println(dummy.y); //calling Non-static variable
      }
      }

      Delete
  2. P:S If you wants to access Instance(non static) variables Inside static method, You need to create object of that class - which is not correct I believe.

    ReplyDelete
  3. please change the color of comment line.

    ReplyDelete