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

  • Home
  • SEARCH
  • 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 868065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:04:49+00:00 2026-05-15T10:04:49+00:00

I understand the following Java outputs. public class EchoTestDrive { public static void main(String[]

  • 0

I understand the following Java outputs.

public class EchoTestDrive {
    public static void main(String[] args){
        Echo e1 = new Echo();
        Echo e2 = new Echo();
        int x = 0;
        while (x<4){
            e1.hello();
            e1.count = e1.count + 1;
            if (x==3){
                e2.count = e2.count +1;
            }
            if(x>0){
                e2.count = e2.count + e1.count;
            }
            x = x+1;
            System.out.println("e1.count is " + e1.count);
            System.out.println("e2.count is " + e2.count);
        }
        System.out.println(e2.count);
    }
}

class Echo {
    int count = 0;
    void hello (){
        System.out.println ("helloooooooo..");
    }
}

Outputs

helloooooooo..
e1.count is 1
e2.count is 0
helloooooooo..
e1.count is 2
e2.count is 2
helloooooooo..
e1.count is 3
e2.count is 5
helloooooooo..
e1.count is 4
e2.count is 10
10

However when I change Echo e2 = new Echo () to Echo e2 = e1, I don’t understand why the outputs is so.

public class EchoTestDrive {
    public static void main(String[] args){
        Echo e1 = new Echo();
        Echo e2 = e1;
        int x = 0;
        while (x<4){
            e1.hello();
            e1.count = e1.count + 1;
            if (x==3){
                e2.count = e2.count +1;
            }
            if(x>0){
                e2.count = e2.count + e1.count;
            }
            x = x+1;
            System.out.println("e1.count is " + e1.count);
            System.out.println("e2.count is " + e2.count);
        }
        System.out.println(e2.count);
    }
}

class Echo {
    int count = 0;
    void hello (){
        System.out.println ("helloooooooo..");
    }
}

Output

helloooooooo..
e1.count is 1
e2.count is 1
helloooooooo..
e1.count is 4
e2.count is 4
helloooooooo..
e1.count is 10
e2.count is 10
helloooooooo..
e1.count is 24
e2.count is 24
24

To me when x = 0, e1.count is 1 and e2.count is 0.
When x = 1, e1.count is e1.count is 2 and e2.count is 2. etc.

I am hoping someone explains it.

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-05-15T10:04:50+00:00Added an answer on May 15, 2026 at 10:04 am

    When you have Echo e2=e1; you make it so both e1 and e2 point to the same memory location. Thus whenever you add to e2 it adds to that memory location so e1 has the same value and vis versa. Specifically

    When x = 0

    e1.hello();
            e1.count = e1.count + 1;   //adds 1 to the memory location
            if (x==3){  // x is 0 so doesn't go in
                e2.count = e2.count +1;
            }
            if(x>0){  // x is 0 so doesn't go in
                e2.count = e2.count + e1.count;
            }
            x = x+1;
            System.out.println("e1.count is " + e1.count);
            System.out.println("e2.count is " + e2.count);
        }
        System.out.println(e2.count);
    }
    

    Thus the memory location equals 1 and both e1 and e2 are 1

    When x = 1

    e1.hello();
            e1.count = e1.count + 1;   
               //adds 1 to the memory location which was already 1 from last time and now equals 2
            if (x==3){  // x is 1 so doesn't go in
                e2.count = e2.count +1;
            }
            if(x>0){  // x is 1 so goes in as 1 is greater than 0
                e2.count = e2.count + e1.count;  // adds e2 and e1 = 2 + 2 from previous line above = 4
            }
            x = x+1;
            System.out.println("e1.count is " + e1.count);
            System.out.println("e2.count is " + e2.count);
        }
        System.out.println(e2.count);
    }
    

    Thus the memory location equals 4 and both e1 and e2 are 4

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

Sidebar

Ask A Question

Stats

  • Questions 477k
  • Answers 477k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Actually, I would say that passing $this to ClassB is… May 16, 2026 at 5:20 am
  • Editorial Team
    Editorial Team added an answer Edit 1: This answer has been improved Edit 2: It… May 16, 2026 at 5:20 am
  • Editorial Team
    Editorial Team added an answer In SQL Server 2008 you can use table value parameters.… May 16, 2026 at 5:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.