Variable Types In Java - WebDriver Tutorials With Java

Before learning close variable types In java software programming language, I recommend you lot to read my posts related to variable's  DATA TYPESACCESS MODIFIERS and STATIC AND NON STATIC VARIABLES. As you lot know, Variable render us a retentiveness infinite (based on type as well as size of declared variable) to shop a value. There are dissimilar three types of variables available In java software evolution language as bellow.

1. Local Variables
  • Variables which are declared Inside methods or constructor are called equally local variables for that method or constructor inward java software programming language. 
  • Variables declared Inside method or constructor are express for that method or constructor only. You tin terminate non access to It exterior that method or constructor.
  • You tin terminate non role access modifiers alongside local variables because they are express to that method or constructor block alone In coffee software evolution language.
  • Need to initialize the value of local variables earlier using It because at that topographic point Is non whatever default value assigned to local variables.
2. Instance Variables (Non Static)
  • Instance variables are declared on degree aeroplane which is parallel to methods or constructors (outside the method or constructor block).
  • Instance variables are by as well as large used alongside objects. So they are created as well as destroyed alongside object creation as well as devastation In coffee software evolution language.
  • Instance(non static) variables are accessible straight past times all the non static methods as well as constructors of that class.
  • If you lot wants to access Instance(non static) variables Inside static method, You needs to practise object of that class.
  • Instance variables are e'er Initialized alongside Its default values based on Its information types.
  • You tin terminate access Instance variable straight (by Its name) Inside same class. If you lot wants to access It exterior the degree as well as hence you lot accept to render object reference alongside variable name.
  • Vast usage of Instance variables inward coffee software programs as well as selenium webdriver software automation tests.
3. Class Variables (Static)
  • Same equally Instance variables, Class variables are declared on degree level (outside the method or constructor block). Only divergence Is class variables are declared using static keyword.
  • Class variables are used In rare instance similar when It Is predefined that value of variable volition never change. In that instance nosotros tin terminate role degree variables.
  • Class variables are created on programme foremost as well as destroyed on programme end.
  • Class variables are visible to all methods as well as constructors of class. Class variables are visible to other degree based on Its access modifiers. Generally they are public.
  • We tin terminate access degree variables straight using Its cite Inside same class. If you lot wants to access It exterior the degree as well as hence you lot ask to role degree cite alongside variable.
Bellow given illustration volition plough over you lot around Idea close all iii types of variables. Created 2 degree to demonstrate you lot how to access degree variable within other class. Both these degree volition demonstrate you lot the access levels of all iii types of variables.

public degree Collage1 {   //Class Variables - Collage cite volition endure same for both departments hence declared equally class(static) variable.  world static String Collage_Name = "A1 Collage";     //Instance Variables  person String Department = "Computer Engineering";  person String name;   person double percentile;  world static void main(String[] args) {//Static Method   //Can access degree variable straight If needed. i.e. Collage_Name   Collage1 student1 = novel Collage1("Robert");   student1.setPercentage(67.32);   student1.print_details();   //Can access Instance variable using object reference If needed.    //Example : student1.name = "Robert";      Collage1 student2 = novel Collage1("Alex");   student2.setPercentage(72.95);   student2.print_details();  }   world Collage1(String student_name){//Constructor   //Can access Instance variable straight Inside constructor.   name = student_name;     }    world void setPercentage(double perc){   //Can access Instance variable straight Inside non static method.   percentile = perc;    }     world void print_details(){   int Year = 2014; //Local Variable - Can non access It exterior 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 volition looks similar 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 degree Collage2 {    person String Department = "Mechanical Engineering";  person String name;   person double percentile;  world static void main(String[] args) {   Collage2 student1 = novel Collage2("Smith");   student1.setPercentage(57.35);   student1.print_details();  }  world Collage2(String student_name){   cite = student_name;     }    world void setPercentage(double perc){   percentile = perc;  }     world void print_details(){    int Year = 2014;   System.out.println("Resultg Of Year = "+Year);   //Can access other class's degree variable using that degree 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 volition looks similar 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% **********************

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