0

I was watching a video on making a game in Java and saw the following code for creating a BufferStrategy object:

public void render()  {
    BufferStrategy bs = getBufferStrategy();
    if bs==null {
       createBufferStrategy(3);
       return
    }
}

I've seen this exact block of code in other examples and videos, so it seems pretty standard. My question is, since neither getBufferStrategy() or createBufferStrategy() are static methods, shouldn't they be called with an object? And since this render method belongs to a class that extends the Canvas class, shouldn't the code look like this?:

public void render()  {
    BufferStrategy bs = this.getBufferStrategy();
    if bs==null {
      this.createBufferStrategy(3);
      return
    }
}

The docs.oracle page for the Canvas class lists these methods as non-static. So why is it ok for us to call them without an object? Any help is greatly appreciated.

Tea
  • 873
  • 2
  • 7
  • 25
robzoid
  • 11
  • 1
    There is no ambiguity, so why do you think you need to use `this`? They are methods which exist either in the class or superclass – Scary Wombat Dec 01 '16 at 01:38
  • I think that because I learned that non-static methods should be called with an object, e.g. obj.meth();. I'm thinking that the 'this' keyword makes it more proper and thus more 'correct', yet the code runs without the 'this' because, as you said, there is no ambiguity. Thanks for the help. – robzoid Dec 01 '16 at 10:06

0 Answers0