0

I'm again here and I like to extend the question I made today about this and super keywords.
Let's take some examples.
Example 1

import java.applet.*;

public class Main extends Applet {
        //overrides Applet.init ()
        public void init () {
                //here I use super keyword without creating an istance of the class Applet
                super.init ();
                //code here...
        }
}

Now let's take an example about this keyword

public class Main { 
        public void print () {
                System.out.println("Hi");
        }

        public static void main (String [] args) {
                //this code instead does not work because I haven't created an istance of the class
                this.print(); 
        }
}

So the question is: I can use super without creating an istance of the class, but to use this I have to create an istance of the class, right?

L99
  • 44
  • 2
  • 5
  • an instance of `Main extends Applet` is also an instance of `Applet` (and ultimately an instance of `Object`). – njzk2 Apr 25 '14 at 16:29
  • `this` never works in a `static` method, because a `static` method is not associated with an instance. – njzk2 Apr 25 '14 at 16:30
  • But i haven't created one using new? In applets the istance is automatically created? – L99 Apr 25 '14 at 16:31
  • if you are in the `public void init()` method, that means there is an instance of `Main`. That instance is also an instance of `Applet`. That's what polymorphism is all about. – njzk2 Apr 25 '14 at 16:37
  • Read about inheritance. Super refers to the class you inherited from, in this case Applet. Main received all methods from Applet, but if you override a method, like init(), but you still want to call the super class init, you have to call super.init() ... Applet is the super class of Main, Main is the sub class of Applet. – ElDuderino Apr 25 '14 at 16:39
  • The applet runner has created an instance of your `Main` class (which *is an* `Applet`) before it calls your `init()` method. Thus, there is an instance, even though you didn't create it; the framework created it. – David Conrad Apr 25 '14 at 16:55

4 Answers4

1

The second example does not work not because you haven't created an instance but because main is static . this is a constant reference to the current object. And because static methods are not related to any objects, and related to the class itself you can't use the this within a static method.

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

Your real problem is that you are calling a non-static and non-declared method within a static method. Because the method is static, there is one copy of all the variables for all instances of the class. Therefore, we don't know which instance is "this".

You can solve the problem by initializing a new instance of your class in main(), and referring to that instance instead of "this".

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0

"super" refers to an instance of the class you are inheritting from. "this" refers to an instance of your current object. The "static" key word means that that method or that attribute can be accessed without creating an instance (object) of that class.

public class Main { 
        public void print () {
                System.out.println("Hi");
        }

        public static void main (String [] args) {
                //this code instead does not work because I haven't created an istance of the class
                this.print(); 
        }
}

In this example you are trying to access a non-static function from an static method. "main" is executed without creating an instance of it, so in order to use any methods or attributes in it you will have to declare them as static. This would work:

public class Main { 
        public static void print () {
                System.out.println("Hi");
        }

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

EDIT: Forgot to remove "this". You cannot access a static method with "this" beacuse it requires an instance of that class.

flotothemoon
  • 1,882
  • 2
  • 20
  • 37
  • Oh sorry, I failed there, didn't notice that. Thank you – flotothemoon Apr 25 '14 at 16:40
  • If I call the method on a non static method using this.print(); will the code work? – L99 Apr 25 '14 at 16:44
  • Depends on from where you call it. If you call it from a static method, no then it wouldn't. You cannot access any non-static attributes (that are defined outside the method itself)/methods from a static method. If you call it from a non-static method it would work, meaning you would have to create an object of that class before. – flotothemoon Apr 25 '14 at 16:46
  • So taking the example of the applet if outside the override of init I declare the non-static method print () and then I call the method inside init (), wich is a non-static method, using this.print(); the code will work, won't it? – L99 Apr 25 '14 at 16:54
  • If you create an instance (object) of that class, yes, then it should. But you would have to create a new class for that because the main method is static, meaning that no object of that class is created. – flotothemoon Apr 25 '14 at 16:56
  • No without the main method. Only an applet that in the init () method calls print using this.print();. The method init isn't static so it will create an istance of the class, won't it? – L99 Apr 25 '14 at 19:32
  • The init method initializes the object, but it doesnt create it. In order to have an instance of that class you have to actually create an object, like Rectangle myRectangle = new Rectangle();. But I'm not sure what you mean to be honest. – flotothemoon Apr 25 '14 at 19:55
  • Yes sorry for my bad English (I'm not English) I create a new question with some code – L99 Apr 25 '14 at 20:14
0

You wouldn't use the "this" keyword in client code (in this case, your main method is the client). It is only used in non-static class methods to refer to the object instance on which that method was called. Think of it as an extra, unspecified parameter to the method, in which a reference to the calling object is passed with the formal parameter name "this".

The "super" keyword refers not to an instance, but to the immediate parent of the class. In this case, the superclass is Applet, as your class Main extends that class. Therefore, when you call super.init() you are calling the init() method of the superclass on "this" to ensure that any work that gets done in that method also gets done when you call your override init() method. You seem to be using it correctly in your code here. You can think of super as a reference to your instance cast as the superclass.

To answer your question, yes, you do need an instance on which to call the method in your client code (main method). However, you wouldn't use the "this" keyword in the client code, you would use the named reference to your actual instance. The "this" keyword is only used inside of the instance method.

wickstopher
  • 981
  • 6
  • 18