LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 8

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

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

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

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

    static void show() {
        System.out.println(this.name);
    }

    public static void main(String[] args) {
        show();
    }
}
  • A name must be final
  • B A static method has no current object, so this cannot be used in it Correct
  • C show must return a String
  • D println cannot print fields
Correct Answer: Option B

Make show an instance method and call it on an object.

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