-5

edits...

  1. Does instantiating a class with an overloaded constructor from main() also call the parameterless constructor?
  2. Do applications that instantiate a plugin class with an overloaded constructor with plugin.execute() necessarily call the parameterless constructor?

to clarify: I'm expecting one answer for main() and one for plugin.execute() entry points.

class pluginObj {

    public pluginObj() {}             // default /primary constructor
    public pluginObj(int altConst) {} // alternate constructor

    public execute() {    //... plugin entry code goes here
    }

    public main() {       //... test entry code goes here
    }
}

context: class is a plugin for another application, using main() for testing in the short term I know that Junit is the correct way to do this in the long run.

Jason K.
  • 407
  • 4
  • 12
  • 1
    Why don't you test it out by printing something in the constructor and seeing what happens. Then add a subclass with constructors to see even more amazing things. – Kayaman Mar 21 '17 at 19:10
  • have you tried this out to see what happens? – ControlAltDel Mar 21 '17 at 19:10
  • 1
    You have 2 constructors in your code: one with a parameter and one without. When you instantiate a pluginObj, only one of the constructors will get called depending on if you supply a parameter or not. – Joe Mar 21 '17 at 19:11
  • 2
    Your question is everything but clear... – Jean-Baptiste Yunès Mar 21 '17 at 19:11
  • What does JUnit suddenly have to do with this question? You don't get to even think about JUnit before you understand how constructors work. – Kayaman Mar 21 '17 at 19:13
  • Thanks for the prompt response(s), I'm using the main() function for testing my class which would usually be called through execute() from another complex application (like any other plugin class). Not that it necessarily matters but I'm working in NetBeans, I was expecting a simple answer with two ordered lists... Here's are similar questions without multiple entry points: can't answer the question... don't downvote. – Jason K. Mar 21 '17 at 19:25
  • Yes I have tested this, the plugin feeds a parameter which is not available for testing from the main() entry which requires code in a constructor (because main is static). I'm thinking the solution may be code which checks a parent class to see if it's running as a plugin or not, but I'd like to find a more elegant solution. – Jason K. Mar 21 '17 at 19:39

1 Answers1

0

Constructors works like this

Public class ExampleClass{
    private int in1;
    private int in2;
    public ExampleClass(){
        this(5) // overloading using some default value
    } // overloads the below and if called super is called here and not below, this then runs the functionality of the below constructor

    public ExampleClass(int in1){
        super(); // hidden call to parent constructor
        this.in1 = in1;
    } // doesn't overload the below or above, and if called simpley sets int1

    public ExampleClass(int in1, int in2){
        super(); // hidden call to parent constructor
        this.in1 = in1;
        this.in2 = in2;
    } // everything is done here, has nothing to do with the other 2 constructors
}

meaning depeiding on how you do the overload ... it always calls the super(), as well as whatever else was defined in the other constructor, also note that 1 instance always get intantiated using only 1 constructor, but as shows may still use the other constructors to provide multiple starting points for classes, it can for instance be used to make classes easier to create, or force some permanent values relevant to the class when it is intantiated.

It's also possible to alter what "super()" call is used see JavaDoc Simply call the super with the arguments of the constructor you wish to use

Mikenno
  • 294
  • 3
  • 9
  • so does super() indirectly call ExampleClass() first? if so can I avoid that? – Jason K. Mar 21 '17 at 19:20
  • @JasonK super() is the constructor for the parent class, and no it is always at the top of any given constructor, also this example asumes that you are overloading by reusing the code you already made (ie don't define every value in every constructor) – Mikenno Mar 21 '17 at 19:23
  • @JasonK. Did this answer the question ? or cn you specify it more ? – Mikenno Mar 21 '17 at 19:39
  • the class is a plugin, which means that the execute() method is called from another application. my goal is to use the main() for testing, that's why I mentioned Junit because I know that's a preferred solution for testing... if my default constructor is empty then there's no issue, but that may not be possible in this case so i'd like to understand if it necessarily needs to execute (and if so in what order). – Jason K. Mar 21 '17 at 19:50
  • @JasonK. define what you mean with "default constructor" if it is the default hidden one, then no it will not nessecarily execute, but the call to the parent constructor "super()" will always, meaning that if you define a constructor as say number 3 above and none other, then only this option is available for instantiating the class – Mikenno Mar 21 '17 at 19:56
  • Sorry I was using the C++ definition of the term default constructor - one without any parameters... We have working code but it's a mess and i'm trying to clean it up, right now i have some statements that are called from the constructor. Another engineer has created extra classes to control which constructor gets called and using custom functions, I feel like this should be more simple but I'm about to give up. – Jason K. Mar 21 '17 at 20:07
  • @JasonK. well, it's possible to move all functions used in the constructor to functions in the class, then create the class and call the functions based on whatever criteria that aplies to diffrent manipulations of the class, the constructor shouldn't have any logic except what is explicitly nessecary for the inital instantiating of the class, say values that have to be set, and therefore can't be null – Mikenno Mar 21 '17 at 20:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138672/discussion-between-jason-k-and-mikenno). – Jason K. Mar 21 '17 at 20:22