Learn Java Basics for Selenium

Java Basics for Selenium

Below are some basics of JAVA useful for Selenium scripts:

  • Class
  • Method
  • Object
  • Sample program
  • Variables
  • Creating Objects
  • If conditions
  • For loops
  • Extending class
  • Arrays


A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.


Methods are nothing but members of a class that provide a service for an object or perform some business logic. Java fields and member functions names are case sensitive. Current states of a class’s corresponding object are stored in the object’s instance variables. Methods define the operations that can be performed in java programming.


An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.

An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables

Sample program:


                                  //Executes if Boolean expression is true
                                 //Executes if Boolean expression is false

For Loop in Java

The for loop is the type of looping construct. It also works as while loop construct but it provide that the initialization, condition and the increment is same written in the for construct. All the statements which has to be executed is written in the for block.

for( initialization; termination; increment)
package com.devmanuals2;
public class ForExample {
          public static void main(String[] args) {
              int i;
                          System.out.println(i+” Times”);


A variable is a container that stores a meaningful value that can be used throughout a program. For example, in a program that calculates tax on items, you can have a few variables – one variable that stores the regular price of an item and an another variable that stores the total price of an item after the tax is calculated on it. Variables store this information in a computer’s memory and the value of a variable can change all through out a program.

Declaring and defining variables

Before using variables, you must declare the variables name and type. See the following example for variables declaration:

int num; //represents that num is a variable that can store value of int type.

String name; //represents that name is a variable that can store string value.

boolean bol; //represents that bol is a variable that can take boolean value (true/false);

You can assign a value to a variable at the declaration time by using an assignment operator ( = ).

int num = 1000; // This line declares num as an int variable which holds value “1000”.

boolean bol = true; // This line declares bol as boolean variable which is set to the value “true“.

Example program:

class PrintText
public static void main(String[] args){
//declare some variables
byte aByte = -10;
int aNumber = 10;
char aChar = ‘b’;
boolean isBoolean = true;
//print variables alone

//print variables with text
System.out.println(“aChar = ” + aChar);
System.out.println(“Is the isBoolean variable a boolean variable? ” + isBoolean);


You extend a class by using the extends keyword in a class declaration,

The Parent class
public class Parent {

The Child class
public class Child extends Parent {

In java, there’s the very important “extend” keyword. A class declaration can use the extend keyword on another class, like this: class C extends B { … }.

When a class C extends class B, C automatically has all variables and
methods defined in class B. (think of it as a internal copying mechanism)

If class C defines a variable or method that has the same name in class B, class C’s definition overrides B’s.

class B {
     int x;
     void setIt (int n) { x=n;}
     void increase () { x=x+1;}
     void triple () {x=x*3;};
     int returnIt () {return x;}
class C extends B {
    void triple () {x=x+3;} // override existing method
    void quadruple () {x=x*4;} // new method

public class GetRich {
    public static void main(String[] args) {
      B b = new B();
      System.out.println( b.returnIt() ); // prints 9
      C c = new C();
      System.out.println( c.returnIt() ); // prints 6
In the above code, class C inherits all class B’s variables and methods. Class C overrides the “triple” method, and added a “quadruple” method.

Other classes can extend Class C, as to inherit all members and methods of class C (which includes those in B). In this way, a tree hierarchy is formed.

When class C extends B, we say that C is a “subclass” of B, and B is the “superclass” of C. This is called inheritence, because C inherited from B. Two or more classes can inherit from the same parent class. However, a class can only have one parent.

In Java, EVERY class is a subclass of java.lang.Object (or subclass of its subclasses). Every class in Java is a node on this giant tree.


An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You’ve seen an example of arrays already, in the main method of the “Hello World!” application. This section discusses arrays in greater detail.


Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

An array is a very common type of data structure where in all elements must be of the same data type.

Once defined , the size of an array is fixed and cannot increase to accommodate more elements.

The first element of an array starts with zero.

0 Responses on Learn Java Basics for Selenium"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.