JAVA : What is a constructor in Java? SDET Automation Testing Interview Questions & Answers

preview_player
Показать описание
JAVA : What is a constructor in Java?

SDET Automation Testing Interview Questions & Answers

We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.

JAVA : What is a constructor in Java?

A constructor in Java is a special method that is called when an object of a class is created.

It is used to initialize the instance variables of the object to their initial values.

A constructor has the same name as the class and no return type. Here's an example of a constructor in Java:

public class Person {
private String name;
private int age;

public Person(String name, int age) {
}

public void sayHello() {
}
}

This defines a constructor for the Person class that takes two arguments, name and age, and initializes the corresponding instance variables using the this keyword.
Рекомендации по теме
Комментарии
Автор

JAVA : What is a constructor in Java?

A constructor in Java is a special method that is called when an object of a class is created.

It is used to initialize the instance variables of the object to their initial values.

A constructor has the same name as the class and no return type. Here's an example of a constructor in Java:

public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public void sayHello() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
}

This defines a constructor for the Person class that takes two arguments, name and age, and initializes the corresponding instance variables using the this keyword.

sdet_automation_testing