You are given a class, `Person`, with a member `name` and a method `greet()`. The `greet()` function returns a greeting message that includes the `name` of the `Person`. However, the `name` must be assigned to an object when this object is created. Fill the blanks in the code to ensure that an instance of the `Person` class can be initiated with a `name`, and that this `name` can be correctly accessed in the `greet()` method.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsClasses and members
Initializing new instances. Constructor
Initializing and accessing class member in a greeting method
Report a typo
Fill in the gaps with the relevant elements
class Person {
String name;
public Person(String name) {
.name = name;
}
public String greet() {
return "Hello, my name is " + name;
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.