Creating and utilizing an object with a string in MyClass

Report a typo

Given a Java class MyClass with a single String member variable str. A single parameter constructor is being created which takes a String as input and assigns it to the instance variable, also a method is present to return the stored string. An object 'myObject' of class 'MyClass' is being initialized with a string. You must fill the blanks in code to ensure the object is created properly, the string is stored correctly, and the stored string is returned to be printed."

Fill in the gaps with the relevant elements
public class MyClass {
     String str;

    public MyClass(String str) {
        .str = str;
    }

    public String returnStr() {
        return this.str;
    }
}

MyClass myObject =  MyClass("Hello, World!");
System.out.println(myObject.returnStr());
privatethisnew
___

Create a free account to access the full topic