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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:29:25+00:00 2026-06-18T02:29:25+00:00

I was reading the certification book of Java 6. And there was an example

  • 0

I was reading the certification book of Java 6. And there was an example about “Shadowing variables”:

package scjp;

class Class1 {
    int number = 28;
}

public class Example {

    Class1 myClass = new Class1();

    void changeNumber( Class1 myClass ) {
        myClass.number = 99; 
        System.out.println("myClass.number in method : " + myClass.number);
        myClass = new Class1();
        myClass.number = 420;
        System.out.println("myClass.number in method is now : " + myClass.number);
    }

    public static void main(String[] args) {
        Example example = new Example();
        System.out.println("myClass.number is : " + example.myClass.number );
        example.changeNumber( example.myClass );
        System.out.println("After method, myClass.number is : " +   example.myClass.number);
    }

}

And this is the result :

myClass.number is : 28
myClass.number in method : 99
myClass.number in method is now : 420
After method, myClass.number is : 99

My question is:
If at the beginning, the variable ‘number’ is 28. When I use the method, it changes the variable to 99 and 420. But …, when the method finish, why does the variable ‘number’ have a value of 99 instead of 28 ?
I thought it would have its original value (28).

  • 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-18T02:29:26+00:00Added an answer on June 18, 2026 at 2:29 am

    When you call changeNumber(), the reference to example is copied and passed to the method. You change the value of number, which modifies the referenced object, then reasssign a new instance to myClass, which does not affect the original reference in main.

    Everything goes as expected, then you exit the method. Back to the main method, you still have the primary reference to example, which was affected by the first reassignment (of number), but not by the reassignment of myClass. That’s why you have 99, not the original 28.

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

Sidebar

Related Questions

I was reading about the java.io.Console class in one of the java certification books,
Reading the Scala by Example book and there is this example when Martin explains
After reading some of the SCJP certification last night, I got thinking about switch
I'm reading Sybex Complete Java 2 Certification Study Guide April 2005 (ISBN0782144195). This book
I was reading A Programmer’s Guide to Java™ SCJP Certification by Khalid Mughal. In
Reading TDPL about function and delegate literals (5.6.1) auto f = (int i) {};
Reading Java Essentials, 2nd edition, there's a rule called PECS for type safety in
I'm studying for the SCJP (inanely over-technical java certification) In my readings, I'm running
reading the SCJP book, I've found something like this in the chapter 1 self-test
Reading a book, and this code comes up: public class Test { private static

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.