How to call a method from Constructor?

0 votes
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.

Nov 13, 2018 in Java by Neha
• 6,260 points
24 views

1 answer to this question.

0 votes

Have a glance at below steps:

  • You create an instance of B.
  • B() calls superclass constructor - A(), to initialize the superclass 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 superclass constructor finishes a job, and returns (Assuming chaining of constructor till Object class have been finished)
  • The B() constructor starts executing further, initializing its own member.
  • Now, as a part of an initialization process, B overwrites the previously written value in testString and re-initializes it to null.

answered Nov 13, 2018 by Frankie
• 9,810 points

Related Questions In Java

0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,150 points
1,216 views
0 votes
1 answer

How to return multiple objects from a Java method?

Yes, we can return multiple objects from ...READ MORE

answered Jul 12, 2018 in Java by Mohini
• 260 points
77 views
0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
85 views
0 votes
1 answer

How to remove ‘char’ from a String?

Hi...here you can try using the overloaded ...READ MORE

answered May 21, 2018 in Java by code.reaper12
• 3,450 points
23 views
0 votes
1 answer

Can Abstract Class have a Constructor?

Yes, an abstract class can have a ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,320 points
62 views
0 votes
2 answers

How to call one constructor from another in Java?

public class Cons { public static Cons ...READ MORE

answered Jul 24, 2018 in Java by Akrati
• 3,150 points
100 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,040 points
38 views
0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,840 points
79 views
0 votes
1 answer

How to create a temporary directory/folder in java?

If you are using JDK 7 use ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,810 points
863 views
+1 vote
1 answer

How to calculate days between two dates?

You are making some conversions with your ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,810 points
41 views