0

Can we use super keyword within the main class in java? What if we use super in the main method. Is it even possible?

class A {
  int a = 5;
}

class B extends A {
  int b = a;

  void method(){
    System.out.println(a+b);
  }
}

class MAIN {
  public static void main(String[] args){
    B obj = new B();
    obj.method();
    // what happens now?
  }
}
TheMP
  • 8,257
  • 9
  • 44
  • 73
user3680847
  • 29
  • 1
  • 1
  • 2
    What exactly would you expect calling `super` in a main method to accomplish? – dimo414 May 27 '14 at 18:00
  • 2
    it's not a duplicate. – TheMP May 27 '14 at 18:04
  • 1
    @Niemand if it's not a duplicate, please expand on what you're asking. At present, it's unclear why you'd want to say `super` in a `main` method, and what you'd expect to be the result of such an operation. – dimo414 May 27 '14 at 18:13
  • @dimo414 it's not me asking, it's just a fragend guy looking for the explanation of what we take for granted :P – TheMP May 27 '14 at 18:15

1 Answers1

0

Nope, we can't. Main method is a static method and therefore it belongs to the class, not to the instance of this class (object). Very good explanation has already been given here - calling a super method from a static method .

Why main method has to be static?

Because non-static main method would be interpreted as just another 'casual' method named 'main'. Your program would not even compile, because it won't be able to find THE static main method. It is perfectly legit to write something like this:

public class Demo {

    public void main() {
        System.out.println("Hello, this is the FAKE main body method");
    }

    public static void main(String [] args) {
        System.out.println("Hello, this is the REAL main body method");
    }
}

After compiling the above code, you would get:

"Hello, this is the REAL main body method"

The FAKE greeting is not printed, "fake" method has to be called after creating object instance, like any other non-static method.

Why can't it be interpretet as THE static main method?

Non-static main method would be ambiguity and JVM won't know how to create object containing it. See this example:

public class Dinosaur {
    public Dinosaur() {
        System.out.println("Hi, I have just created a dinosaur!");
    }
    public void main() {
        System.out.println("Hello, this is the main body method");
    }
}

Should it call parameterless constructor? Or should it just skip it and run the code from main method and don't construct this object? What should running this class print first - creating a Dinosaur or welcoming you in the body of main? Or maybe just execute the main and don't create an object? You have a whole lot of options here.

As you see, due to this type of inconvenience interptering every method named main as THE main would cause pure chaos. Also, thanks to the static main method you can have an entry point, a place where the whole program begins and cannot be shadowed by some lousy parameterless constructors :)

Community
  • 1
  • 1
TheMP
  • 8,257
  • 9
  • 44
  • 73
  • What if main method is not static? – user3680847 May 27 '14 at 17:58
  • 1
    If main() is not static, then it really isn't acting like a main method. – Edwin Torres May 27 '14 at 17:59
  • Main method has to be static, otherwise it would not be a main method and just some nomal method named 'main'. Non-static main method would be ambiguity and JVM won't know how to create object containing it. – TheMP May 27 '14 at 18:00
  • 2
    Note there's nothing "wrong" with creating a non-static method called `main`, but the JVM will simply not consider it while looking for the entry point into your program. Doing so would be needlessly confusing, though. – dimo414 May 27 '14 at 18:02
  • @Niemand -Your answer doesn't explain answer to the main part, and For Your Kind Information,my answer also resembles your's in terms of explanation of second part. Both are synonymous,practical from the viewer's point. Rather, I would have downvoted your answer,had I got the privilege to do so. Your answer doesn't give a sensible and complete information to the questionnaire!!! – Am_I_Helpful May 27 '14 at 19:43
  • @dimo414 That's what I tried to explain, hope I made it quite clear. – TheMP May 27 '14 at 19:49
  • I already mentioned that it is more practical from the reader's view. And,do you disagree that you did not answer the first part of the question? – Am_I_Helpful May 27 '14 at 19:49
  • This answer is not at all appropriate!!! – Am_I_Helpful May 31 '14 at 19:54