Suppose you have created a class in Java and you want to have a message that can be accessed without instantiating this class. Also, you want this message to be unchanged in different instances of the class. Fill the blanks in code to create such a class with a 'getStaticMessage' method that returns this message and a main method to print this returned message.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsClasses and members
Static members
Creating a static message in a class
Report a typo
Fill in the gaps with the relevant elements
class MyStaticClass {
String MESSAGE = "Hello, world!";
public MyStaticClass() {
}
public static String getStaticMessage() {
return MESSAGE;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MyStaticClass.getStaticMessage());
}
} ___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
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.