#what does the keyword this do exactly?

7 messages · Page 1 of 1 (latest)

gloomy rain
#
public class MyFrame extends JFrame {
   MyFrame() {
        MyPanel panel = new MyPanel();
        this.add(panel);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.pack();
        this.setVisible(true);
    }
} ```

So I'm looking at this right now and a little bit confused. What I knew about "this" was the fact that it can be used to not confuse java when you have a variable with the same name in the field and again in another method. In this case why are we allowed to use this without any object?
deep mountainBOT
#

This post has been reserved for your question.

Hey @gloomy rain! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

pliant folio
#

You do have an object. You're in a constructor

prime kernel
#

a constructor is just a special kind of method, this refers to the newly created instance of MyFrame, in your case

pliant folio
#

"special kind of object?" A constructor is something that runs on a partially constructed object, that finishes its construction per the spec of the current class

prime kernel