Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:29:58+00:00 2026-05-30T08:29:58+00:00

//src/com/test/animal/Animal.java package com.test.animal; public class Animal { Animal() { init(); } public void init()

  • 0
//src/com/test/animal/Animal.java
package com.test.animal;

public class Animal
{
    Animal()
    {
        init();
    }

    public void init()
    {
        System.out.println("parent init()");
    }
}

//src/com/test/animal/Dog.java 
package com.test.animal;

public class Dog extends Animal
{
    String name = null;

    Dog()
    {
        super();
    }

    public void init()
    {
        System.out.println("child init()");
        super.init();
        name = new String("dog");
        System.out.println("name: "+name);
    }

    public static void main(String[] args)
    {
        Dog d = new Dog();
        System.out.println("name: "+d.name);
    }
}

The output is:

child init()
parent init()
name: dog
name: null

It seems the init() in child is called, but NAME value not saved! Why?
It would be OK if I move NAME to parent. However, it’s more resonable to retain in child, since it’s Dog-specific.

Also, I can explicitly call init() in child’s constructor to solve this issue. It’s not that good.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:29:59+00:00Added an answer on May 30, 2026 at 8:29 am

    The order in which this executes is as follows:

    1. The Dog constructor is called.
    2. It calls the Animal constructor.
    3. The Animal constructor calls the init method. Because it is overridden in Dog, the Dog version is called.
    4. In Dog.init you set name to "dog".
    5. The Dog.init method returns.
    6. Now, the member variables of Dog are initialized. This sets name to null.

    Result: name will be null.

    An excellent example of why you should not call methods that can be overridden from a constructor – because it leads to surprises like this.

    Side note: Never do this:

    // Unnecessarily creating a new String object
    name = new String("dog");
    

    Just do this instead:

    name = "Dog";
    

    It is never necessary to explicitly create a new String object from a string literal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my source in the hierarchy: folder: src/main/java package: com.test.serviceImpl Now eclipse is
I have java class files and property files in the same directory. src/main/java/com/berlin/Test.class src/main/java/com/berlin/Test.properties
My Java application has got a package structure similar to this: src/com/name/app src/com/name/app/do src/com/name/utils/db
I made a really simple java example with two files: Dog.java contains: package com.greg.dog;
I have a <img> : <img src=http://example.com/mmm/01.jpg class=test> How can I check if the
I have a java app, and the log4j.properties file is in src/com/my/path/props. On compile,
<Window x:Class=MyWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:src=clr-namespace:WpfApplication1 Title=ContactsSelector Height=300 Width=300> <Window.Content> <src:MyPage> <!--MyPage is a page
I have a git repository that is just a directory tree (/myprogram/src/com/mycompany/test) with some
I have got to know that src/test/java is used for writing unit tests. I
Look at this situation: www.websitea.com displays an img tag with a src attribute of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.