0

I'm sorry, maybe it's simple, but I can't explain this. There is following code(swing):

public class Sandbox2 extends Frame implements ActionListener {  
    JTextField tf; JLabel l; JButton b;  

    Sandbox2() {  
        tf=new JTextField();  
        ...   

        //there is what i can`t understand
        add(b);add(tf);add(l);    
        setSize(400,400);  
        setLayout(null);  
        setVisible(true);  
    }

    public void actionPerformed(ActionEvent e) {  
        ... 
    }  

    public static void main(String[] args) {  
        new Sandbox2();  
    } 
}

Firstly I create a button, text field and others, after that I add it to a frame. But how it works if all methods, add(), setSize()... were called without frame instance?

I understand that it due with a Frame inheritance, but how?

Jason
  • 11,744
  • 3
  • 42
  • 46
Yonshoku
  • 115
  • 1
  • 7
  • 3
    Because you have a frame—your class is-a frame. This is fundamental to how Java OOP works. – Dave Newton Jan 12 '20 at 22:14
  • 1
    typo it's `JFrame` – Exteam Jan 12 '20 at 22:14
  • 3
    You do have an instance. It's called `this`, and it is used implicitly. – user207421 Jan 12 '20 at 22:25
  • Depending on if method is static or not, compiler can let us skip `this.` or `CurrentClassName.` parts in `this.nonStaticMethod()` and `CurrentClassName.staticMethod()` calls respectively. Inside a method `this` keyword returns reference to instance *on which method was called*, inside a constructor `this` returns reference to currently constructed object. – Pshemo Jan 12 '20 at 22:28
  • The anonymous method containing the calls in questions is a constructor for your `Sandbox2` class. Its called in the main method with `new Sandbox2()`. Although not shown, the first thing the constructor does is call the constructor for the Frame class. The created instance can be referenced by `this` and you could preface the rest of the method calls in the constructor with `this.` as `this.add(b);`. – Michael McKay Jan 12 '20 at 23:12
  • Does this answer your question? [Static Classes In Java](https://stackoverflow.com/questions/7486012/static-classes-in-java) – Austin Jan 12 '20 at 23:12
  • 1
    @Austin No it doesn't. There are no static classes here. Strange suggestion. – user207421 Jan 12 '20 at 23:33

3 Answers3

4

All of these calls are happening inside the context of an instance. If you just call setSize, then that is the same as saying this.setSize. (If you tried to call setSize in a static method, which specifically means that it is not linked to a specific instance, then you would get an error.)

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

When you construct an instance of a class that extends another class, the parent constructor is called first, setting up anything that it needs, before your local constructor is called (either implicitly, or by calling super() in the first line of your constructor).

So, since Sandbox2 is-a JFrame (extends the class), the JFrame stuff will be ready by the time the code in your Sandbox2 constructor is executed.

Jason
  • 11,744
  • 3
  • 42
  • 46
0

The class SandBox2extends JFrame meansSandBox2 is-a JFrame class. The SandBox2class inherits the following methods from the super class JFrame. Hence can be called like add(b)

add(b);
add(tf);
add(l);    
setSize(400,400);  
setLayout(null);  
setVisible(true); 

As per the Doc As a convenience add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write: frame.add(child);

SandBox2 uses inheritance by which class allows to inherit the features(fields and methods) of another class. The subclass can add its own fields and methods in addition to the superclass fields and methods.

fabfas
  • 2,200
  • 1
  • 21
  • 21
  • This doesn't actually answer the question. – user207421 Jan 12 '20 at 22:47
  • @user207421 The `SandBox2` is-a `JFrame` class. For that reason, you can call `add(..)` and other methods without instance of super class. – fabfas Jan 12 '20 at 22:51
  • @Pshemo Yes, those methods are not static and called on instance of class because `SandBox2` is-a `JFrame` class. – fabfas Jan 12 '20 at 22:54
  • 1
    @fabfas Of course, but the OP thinks he doesn't have an instance, and he does, and your statement 'without instance of superclass' is therefore incorrect. – user207421 Jan 12 '20 at 23:25
  • I may misunderstood your answer previously so I removed my comment, but still could you explain what do you mean by "The following code gets called in the super class"? That code is executed *in* constructor *at* instance of `Sandbox2` which is being initialized by that constructor. To access that instance we can use `this` keyword like `this.add(b);` and that is the code which compiler generates while compiling it. It is true that `add` method and rest of them is inherited *from* supertype but it still is invoked *on* instance of `Sandbox2` which is being initialized with that constructor. – Pshemo Jan 13 '20 at 01:10
  • @Pshemo - sorry for the confusion. I was trying to say the code is part of the super class. It's true the methods are inherited and `Sandbox2` is-a `JFarme` and actually calling on the instance of `Sandbox2` – fabfas Jan 13 '20 at 11:54