LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Find the Error · Question 7

Error: using this inside a static method Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    String site = "LearnFrenzy";

    static String banner() {
        return "Welcome to " + this.site;
    }

    public static void main(String[] args) {
        System.out.println(banner());
    }
}
  • A site must be final
  • B banner is static, so there is no current object and this cannot be used Correct
  • C Strings cannot be concatenated with fields
  • D banner must be private
Correct Answer: Option B

Pass the object in, or make banner an instance method.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links