-3

enter image description here

I was trying to get this java program to return 2. Why can't I type wait(); in main to do this?

I have an unrelated constructor (to this task of returning 2), and I've tried making an object in main to make it work but that didn't help.

Would a related constructor or method help? Do I have to initialize variables at the top of the class to accomplish this every time? Thanks for the help

  • 3
    Please do not post images of code (or errors). Instead, add the code to the question itself as code-formatted text. The example code should preferably be a [mre]; case in point, I can't see anything wrong with the method shown in your image which likely means the problem is in code you haven't provided. Also, please include the _exact error message_ you're getting (also as text, not an image). You can [edit] your question to make improvements. – Slaw May 16 '20 at 06:01
  • 1
    Turns out the image does show at least one problem, as demonstrated by QuickSilver. However, for future reference, please make sure to provide a [mre] demonstrating the problem, the exact error(s) you're getting, and in general make sure your question meets the standards outlined in [ask]. Remember, ideally your question should also be helpful to others in the future, not just yourself right now. – Slaw May 16 '20 at 06:11

1 Answers1

3

Below are the problems with the code in the image

  • By default all class eventually end up extending Object class
  • You cannot override a final method. wait() is final method in Object class
  • Even though static method are class levels methods they cannot override instance method

Below is the method from Object class

public final void wait() throws InterruptedException {
        wait(0);
    }
QuickSilver
  • 3,915
  • 2
  • 13
  • 29