Java why does super have to be first




















I remember I needed that. Can you prove that I did something meaningless? The door is locked, so you smash a window, reach in and let yourself in. Inside, halfway across the room, you find a note with a key for you to use on the way in. But you're already in. Similarly if the compiler is halfway through executing a constructor and it comes across "here's what to do with those parameters before you run the constructor" what is it supposed to do?

If it is stupid thing in reality then it is a wrong analogy. If I am in a position to decide, which way to go, I am not in the half way. It is the rule that the supercall must be the first in constructor that prvoces us to break the window see a lot of examples of warkingaround in the question and answers instead of using the door. So, you put everything upside down when trying to argue for this rule. The rule must be wrong, therefore. Tip-Sy Tip-Sy 9 9 silver badges 18 18 bronze badges.

This works only if the super class constructor expects a single, non-void argument — KrishPrabakar. So maybe OP is inquiring why it was not done this way. DaveFar DaveFar 6, 3 3 gold badges 41 41 silver badges 84 84 bronze badges. I think the downvote is because you're not answering the question, but making comments on the issue. Excellent example of the ways the? I was thinking as I read, "It's not impossible just use a ternary oper I found a woraround.

Vouze Vouze 1, 16 16 silver badges 10 10 bronze badges. The question is not about a workaround. In fact a work around can be found in the question itself. This is not a workaround. You still can't write multi lines of code. Making everyone do this extra work probably seemed like a greater cost than benefit. Sai Kishore 1 1 gold badge 9 9 silver badges 16 16 bronze badges.

Dobes Vandermeer Dobes Vandermeer 7, 4 4 gold badges 42 42 silver badges 43 43 bronze badges. Writing a sane grammar for the feature would in itself be quite hard - such a grammar would match a statement tree where at most one leaf node is an explicit super-constructor call.

I can think of a way to write it, but my approach would be quite insane. You may say that ignoring exceptions is always a bad idea. How many objects are we creating in this example? Or maybe nothing In such cases, you have the following opportunities: Use the pattern presented at this answer , which allows to circumvent the restriction.

Wait for the Java team to allow pre- super and pre- this code. It may be done by imposing a restriction on where super or this may occur in a constructor. Actually, even today's compiler is able to distinguish good and bad or potentially bad cases with the degree enough to securely allow static code addition at the beginning of a constructor. Indeed, assume that super and this return this reference and, in turn, your constructor has return this; at the end.

Make the constructors private. Make static methods associated with the constructors. Let us call the class Foo, 2 constructors, Foo and Foo int i , and static methods that constructs it, createFoo and createFoo int i. Then replace this with Foo. Therefore, you can do stuff in createFoo int i and lastly do Foo. Or any other order. It is sort of like a factory design pattern, but not.

Savvas Dalkitsis Savvas Dalkitsis But this adds nothing over just adding the System. What would be useful would be a way to execute code before the parent is constructed. If you will try to add something, you'll need so save the computed state somewhere. Even if compiler allows you to, what will be the temporary storage? Allocate one more field just for initialization? But this is waste of memory.

This is incorrect. Instance initializers are inserted after the parent constructor call returns. Jaydev Jaydev 1, 18 18 silver badges 36 36 bronze badges. In this example, there is default constructor in each class and so no emergency need to call super Farrukh Ahmed Farrukh Ahmed 1 1 silver badge 1 1 bronze badge.

Tldr: The other answers have tackled the "why" of the question. I'll provide a hack around this limitation: The basic idea is to hijack the super statement with your embedded statements. Get ; t2. They'd just chosen not to. Community Bot 1 1 1 silver badge.

Pacerier Pacerier Oleksandr Oleksandr 2, 7 7 gold badges 33 33 silver badges 64 64 bronze badges. Raslanove Raslanove 8 8 silver badges 15 15 bronze badges. Adarsh Verma Adarsh Verma 1. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. So whenever I create a new object in Java, the constructor of the Object class is actually run first.

However, Java does check to see if you want to run a different constructor than the default. It looks at the first line of the constructor to see if their is a reference to super or this before it goes up the hierarchy.

I got most of what is being said here. Winston Gutkowski. I like So the answer is as it is to most 'why' questions like this : I don't know. Presumably the Founding Fathers had their reasons; but without a Vulcan mind-meld with one of them, we can only speculate. My guess is: Because it was simplest. Winston PS: You do understand that you've revived a year old thread?

It's no big deal; but I suspect it's not far off a record. Glad to hear it. And good luck with your studies. About the only piece of advice I can give you is that "why" questions of this type are rarely productive, and are even less likely to produce the sort of definitive answer you want.

Java, like any other language, has its "quirks"; and not all of them can be explained away by expertise. If you want me to elaborate on my "guess": I suspect that the logic required for the compiler to allow local variables to be declared and restricting those declarations was more than they fancied taking on in version 1; so they replaced it with a simple rule: Constructor calls MUST be the first thing you do.

Since then, programmers have learned to live with it; so why change? That's my guess. But it's only a guess. Paul Clapham. I learned object-oriented programming in a language which allowed you to call the superclass constructor anywhere in your constructor, and this was quite handy. If anyone is going to try and compromise a program, then a constructor is one of the best places to do it; so the more chances you give a programmer to make a mistake, that's where your vulnerability is going to be.

The super keyword can also be used to call functions on a parent object. This code snippet is taken from the classes sample live demo. Here super is called to avoid duplicating the constructor parts' that are common between Rectangle and Square.

You are also able to call super on static methods. Hide TOC. Interfaces and Inheritance. Multiple Inheritance of State, Implementation, and Type. Accessing Superclass Members If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.



0コメント

  • 1000 / 1000