2

I have the following code:

public class Application extends ApplicationManager{

    public static void main(String[] args) {
        ProcessUtility.enableProcessUtility(); 
        new Application().start();
    }
}

and the class ApplicationManager code:

public class ApplicationManager {

public ApplicationManager() {

   String configPath = "file:home" +  File.separator + "log4j.xml";
   System.setProperty("log4j.configuration", configPath);
   logger = Logger.getLogger(ApplicationManager.class);
 }

protected void start() {
    logger.info("*** Starting ApplicationManager ***");
}

when I run the application class the start method of the parent will be called, can it be called without calling the parent default constructor?

my second question is the above code different from this code:

public class Application extends ApplicationManager{

    public static void main(String[] args) {    
        new Application().start();
    }


    @Override
    protected void start() {
        ProcessUtility.enableProcessUtility();
        super.start();
    }
}

and the ApplicationManager class as above.

this is the code of the static method:

public static void enableProcessUtility() {
    isCommon = false;
}

thanks in advance.

flashDisk
  • 55
  • 5

2 Answers2

4

Calling a non static method (your start method) requires creating an instance of the class that contains the method (or a sub-class of that class). Creating an instance of a class requires invoking the constructors of the class and all its ancestor classes. Therefore you can't avoid executing the parent default constructor.

As for the second question, moving ProcessUtility.enableProcessUtility() to the sub-class's start method means that it will be executed each time you call the start method.

That said, in your example your main only creates one instance of Application and only calls start once for that instance. Therefore ProcessUtility.enableProcessUtility() will only be executed once in both snippets, and the behavior would be identical.

EDIT: Another difference between the two snippets is that the first snippet calls ProcessUtility.enableProcessUtility() before creating the Application instance, while the second snippet first creates the instance and then calls ProcessUtility.enableProcessUtility() from within start of the sub-class. If the behavior of the constructors (of either the sub class or the super class) is affected by the call to ProcessUtility.enableProcessUtility(), the two snippets may produce different outputs.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • when i run the two codes, I get differnet results how can that happen?, I know it must call the parent constructor but some code of the parent constructor is not running in one of these implementations! – flashDisk Sep 01 '16 at 09:32
  • @flashDisk In the second snippet the constructors are executed prior to `ProcessUtility.enableProcessUtility()`. Perhaps that's the cause for the different behavior. – Eran Sep 01 '16 at 09:34
  • the correct behavior is the second implementation, but still I can't find why! – flashDisk Sep 01 '16 at 09:40
  • @flashDisk I can't answer that without seeing the code of the constructors ,`ProcessUtility.enableProcessUtility()` and `start()`. – Eran Sep 01 '16 at 09:41
  • the code is updated, the difference is that in one implementation the log4j prints the above line to the console and in the second it does not!. – flashDisk Sep 01 '16 at 09:48
  • @flashDisk How is the `isCommon` variable used? Perhaps it affects the logging somehow. – Eran Sep 01 '16 at 09:50
  • there is a method that checks if it is true and run a script in the system, I don't think it affects that. – flashDisk Sep 01 '16 at 11:00
1

Your first question is answered here https://stackoverflow.com/a/10508202/2527075

As for your second question, the super constructor will be called before the ProcessUtility call in the second example, where in the first example the ProcessUtility call comes first.

Community
  • 1
  • 1
  • when the constructor of the parent is called?, when calling one of it's methods? or when calling the constructor of the sub class? – flashDisk Sep 01 '16 at 09:55
  • Every constructor first does an implicit super(); call. So every subclass constructor first calls the super constructor. – Silvan Van Leeuwen Sep 01 '16 at 13:51