Why can not I make use of a shot block around my extremely () call?

So, in Java, the first line of your erector HAS to be a phone call to extremely ... be it unconditionally calling extremely (), or clearly calling an additional erector. What I need to know is, why can not I placed a shot block around that?

My details instance is that I have a simulated class for an examination. There is no default erector, yet I desire one to make the examinations less complex to read. I additionally intend to cover the exemptions tossed from the erector right into a RuntimeException.

So, what I intend to do is properly this:

public class MyClassMock extends MyClass {
    public MyClassMock() {
        try {
            super(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Mocked methods
}

But Java whines that extremely isn't the first declaration.

My workaround:

public class MyClassMock extends MyClass {
    public static MyClassMock construct() {
        try {
            return new MyClassMock();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public MyClassMock() throws Exception {
        super(0);
    }

    // Mocked methods
}

Is this the most effective workaround? Why does not Java allow me do the previous?


My ideal hunch regarding the "why" is that Java does not intend to allow me have actually a created object in a possibly irregular state ... nonetheless, in doing a simulated, I uncommitted concerning that. It appears I need to have the ability to do the above ... or at the very least I recognize that the above is secure for my instance ... or appears as though it needs to be anyways.

I am bypassing any kind of approaches I make use of from the examined class, so there is no threat that I am making use of uninitialized variables.

0
2019-05-13 02:38:54
Source Share
Answers: 3

Unfortunately, compilers can not work with academic concepts, and also despite the fact that you might recognize that it is secure in your instance, if they permitted it, it would certainly need to be secure for all instances.

To put it simply, the compiler isn't quiting simply you, it is quiting every person, consisting of all those that do not recognize that it is harmful and also requires special delivery. There are possibly various other factors for this too, as all languages generally have means to do harmful points if one recognizes just how to manage them.

In C#. NET there are comparable stipulations, and also the only means to proclaim an erector that calls a base erector is this:

public ClassName(...) : base(...)

in doing so, the base erector will certainly be called prior to the body of the erector, and also you can not transform this order.

0
2019-05-17 12:22:36
Source

I can not assume to have a deep understanding of Java internals, yet it is my understanding that, when a compiler requires to instantiate an acquired class, it needs to first create the base (and also its base prior to that (...)) and afterwards add the expansions made in the subdivision.

So it is not also the threat of uninited variables or anything like that in all. When you attempt to do something in the subdivision' erector prior to the base class' erector , you are primarily asking the compiler to expand a base object instance that does not exist yet.

Modify :In your instance, MyClass comes to be the base object, and also MyClassMock is a subdivision.

0
2019-05-17 12:18:09
Source

I do not recognize just how Java is applied inside, yet if the erector of the superclass tosses an exception, after that there isn't a instance of the class you expand. It would certainly be difficult to call the toString() or equals() approaches, as an example, given that they are acquired most of the times.

Java might permit a try/catch around the extremely () call the erector if 1. you bypass ALL approaches from the superclasses, and also 2. you do not make use of the super.XXX () condition, yet that all audios also made complex to me.

0
2019-05-17 12:07:22
Source