Usage of Constructors | Default Constructor|Parameterized Constructor | Java | Selenium WebDriver

Опубликовано: 17 Сентябрь 2020
на канале: total-qa
851
13

Constructor:
============
Constructor is a method which is having same name as class name and it doesnt have return type and return value.

public Class Car
{

//Constructor
public Car()
{

}
}

1. Constructors are invoked automatically whenever we create an object.
How to create objects using classname.
ClassName object = new ClassName();

2. What happens if we do not write constructor. Compiler automatically invokes a constructor which is internally invoked not seen by the user.

3. Constructors are useful to initialize the variables in the class.

4. They are two types of constructors in java.

a. Default Constructor
b. parameterized Constructor.

In order to invoke the parameterized constructor we have to pass the arg while creating the object

Ex: ClassName Object = new ClassName(arg); this invokes the parameterized constructor.
File f =new File("inputfile.txt");

this keyword is useful to distinguish between global and local variables
this is referring to current object in execution.
Ex: this.rollno = rollno;
this.element = element;