Calling method from constructor

StrangeWill :

Excuse any minor syntax errors or whatnot, I'm experiencing this with a Jitsi module and not being super familiar with Java want to confirm what is going on and why and how it should be fixed.

 public abstract class A
{
  public A()
  {
    this.load();
  }

  protected void load()
  {

  }
}

public class B extends A
{
  private String testString = null; 

  public B()
  {
    super();
  }

  @Override
  protected void load()
  {
    testString = "test";
  }
}

The application is doing this when creating an instance of the class B using a load class by name method:

  • Calls overridden load() in class B
  • Initializes variables (calls "private string testString = null" according to debugger), nulling them out.

Is this expected Java behavior? What could cause this? It's a Java 1.6 application running on the 1.7 JDK.

Rohit Jain :

Is this expected Java behavior?

Yes.

What could cause this?

Your invocation of non-final overridden method in non-final super class constructor.

Let's see what happens step-by-step:

  • You create an instance of B.
  • B() calls super class constructor - A(), to initialize the super class members.
  • A() now invokes a non-final method which is overridden in B class, as a part of initialization.
  • Since the instance in the context is of B class, the method load() invoked is of B class.
  • load() initializes the B class instance field - testString.
  • The super class constructor finishes job, and returns (Assuming chaining of constructor till Object class have been finished)
  • The B() constructor starts executing further, initializing it's own member.
  • Now, as a part of initilization process, B overwrites the previous written value in testString, and re-initializes it to null.

Moral: Never call a non-final public method of a non-final class in it's constructor.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling virtual method from a constructor

From Javascript

Calling a class method from the constructor

From Java

Calling an overridden method from a constructor

From Java

Calling a Method from Constructor Method - Java

From Dev

Calling a constructor from method within the same class

From Dev

Safe alternative to calling of abstract method from constructor

From Dev

Calling constructor from a subclass method with a generic parameter

From Dev

Calling a method on a constructor method

From Dev

Calling a void Method into a constructor

From Dev

Calling a constructor as a method

From Dev

Calling action from constructor vs a life cycle method

From Dev

React Native Issue - Initialize state by calling another method from constructor

From Dev

Calling a method from the constructor in Angular2/IONIC2

From Dev

Calling a method of a function constructor from another function in the global scope - Javascript

From Dev

Calling ES6 class constructor from class static method

From Dev

Calling an instance method from a lambda given as an argument to a constructor

From Dev

Calling constructor from shared method (C# to VB.Net)

From Dev

Calling private method from inside the constructor does not change values of variables

From Java

Calling Constructor from another Constructor

From Java

calling setters from a constructor

From Dev

Calling constructor with "()" is different from "{}"

From Dev

Calling a value from a constructor

From Dev

Calling a javascript constructor functions method

From Dev

Calling constructor of subclass from constructor of superclass

From Dev

Calling a protected constructor from a derived constructor

From Dev

Calling global function from constructor

From Dev

Calling the constructor from the assignment operator

From Java

Calling Parameters from parent constructor

From Dev

calling a constructor from a derived class

Related Related

  1. 1

    Calling virtual method from a constructor

  2. 2

    Calling a class method from the constructor

  3. 3

    Calling an overridden method from a constructor

  4. 4

    Calling a Method from Constructor Method - Java

  5. 5

    Calling a constructor from method within the same class

  6. 6

    Safe alternative to calling of abstract method from constructor

  7. 7

    Calling constructor from a subclass method with a generic parameter

  8. 8

    Calling a method on a constructor method

  9. 9

    Calling a void Method into a constructor

  10. 10

    Calling a constructor as a method

  11. 11

    Calling action from constructor vs a life cycle method

  12. 12

    React Native Issue - Initialize state by calling another method from constructor

  13. 13

    Calling a method from the constructor in Angular2/IONIC2

  14. 14

    Calling a method of a function constructor from another function in the global scope - Javascript

  15. 15

    Calling ES6 class constructor from class static method

  16. 16

    Calling an instance method from a lambda given as an argument to a constructor

  17. 17

    Calling constructor from shared method (C# to VB.Net)

  18. 18

    Calling private method from inside the constructor does not change values of variables

  19. 19

    Calling Constructor from another Constructor

  20. 20

    calling setters from a constructor

  21. 21

    Calling constructor with "()" is different from "{}"

  22. 22

    Calling a value from a constructor

  23. 23

    Calling a javascript constructor functions method

  24. 24

    Calling constructor of subclass from constructor of superclass

  25. 25

    Calling a protected constructor from a derived constructor

  26. 26

    Calling global function from constructor

  27. 27

    Calling the constructor from the assignment operator

  28. 28

    Calling Parameters from parent constructor

  29. 29

    calling a constructor from a derived class

HotTag

Archive