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 8464315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:42:32+00:00 2026-06-10T14:42:32+00:00

I have this demo code: class Test2 extends Test { public int number =

  • 0

I have this demo code:

class Test2 extends Test {
    public int number = 0;

    @Override
    public void set(){
        number = 1;
        info();
    }

    @Override
    public void info(){
        System.out.println(number);
    }
}

public class Test {
    public Test(){
        set();
    }

    public void set(){
    }

    public void info(){
    }

    public static void main(String[] args){
        Test2 object = new Test2();
        object.info();
    }
}

The code gives this output:

1
0

Why? I expect this output:

1
1

In my opionion, the main function calls a constructor of Test2 class to create an object. The constructor calls automatically superclass’ constructor. This constructor calls method set() which is overridden. Therefore method set() of class Test2 is called. This method sets the field and calls info() method which writes the number. Then the main function calls the info() method of the created object again.

The number field is correctly set as the first line output is “1”. But why the second line contains 0? It seems that the field was not set at all. Can you explain it?

What should I do to get the behaviour I expect? Thanks in advance!

  • 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-06-10T14:42:34+00:00Added an answer on June 10, 2026 at 2:42 pm
    class Test2 {
        public int number = 0;
    }
    

    is equivalent to

    class Test2 {
        public int number;
    
        public Test2() {
            super();
            number = 0;
        }
    }
    

    So through invoking the super constructor the field number is set to 1. After the return from the super constructor, your assignment of number to 0 is executed.

    Just remove the assignment and it should behave as you expect.

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

Sidebar

Related Questions

I have set up a demo on this page : http://permanaj.net/jqgrid/index.html code: <html> <head>
I have the following: <p>This is a test</p> <pre>public class Car { protected Car()
I have set the Masonry demo on this page . I see that the
I'm working on legacy code that looks very similar to this: public class BaseParser
EDIT: In case you want to test i have deployed the code to this
see this demo from jquery ui you have to hold down the Ctrl key
Please have a look at this demo page that I cooked up a while
So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
I have this xml <products> <product> <name>Demo</name> </product> <product> <name>Black Beauty III</name> <description>Beautiful les
I am trying to implement this demo on my local machine. http://bit.ly/4g4o1r I have

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.