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

preview_player
Показать описание

Level up your SDET and QA skills! 🚀 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?

In Java, a constructor is a special method that is used to initialize objects of a class. It has the same name as the class and does not have a return type.

When an object is created using the new keyword, the constructor is called automatically to initialize the object's instance variables.

Here's an example:

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

public Person(String name, int age) {
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}

public class Main {
public static void main(String[] args) {
Person person = new Person("John", 30);
}
}

This defines a class called Person with instance variables for name and age, and a constructor that takes two arguments and initializes the instance variables.

In the main method, a Person object is created with the name "John" and age 30, and the getName and getAge methods are called on it to print the following output:

John is 30 years old.
Рекомендации по теме
Комментарии
Автор

JAVA : What is a constructor in Java?

In Java, a constructor is a special method that is used to initialize objects of a class.

It has the same name as the class and does not have a return type.

When an object is created using the new keyword, the constructor is called automatically to initialize the object's instance variables.

Here's an example:

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

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

public String getName() {
return name;
}

public int getAge() {
return age;
}
}

public class Main {
public static void main(String[] args) {
Person person = new Person("John", 30);
+ " is " + person.getAge() + " years old.");
}
}

This defines a class called Person with instance variables for name and age, and a constructor that takes two arguments and initializes the instance variables.

In the main method, a Person object is created with the name "John" and age 30, and the getName and getAge methods are called on it to print the following output:

John is 30 years old.

sdet_automation_testing
welcome to shbcf.ru