Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within foundations
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830
this week in the
Programmer Certification
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Java Foundations Exam
Shadowing in Java
Tiam Bezalel
Ranch Hand
Posts: 67
posted 2 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am preparing for the
1Z0-808
exam and I would like to understand
shadowing in
java
. Please help me to understand why this code produces "99" at the end.
/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package shadowing; /** * * @author xxxxxx */ public class Bar { int barNum = 28; }
/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package shadowing; /** * * @author xxxxx */ public class Foo { Bar myBar = new Bar(); void changeIt(Bar myBar) { myBar.barNum = 99; System.out.println("myBar.barNum in changeIt is " + myBar.barNum); myBar = new Bar(); myBar.barNum = 420; System.out.println("myBar.barNum in changeIt is now " + myBar.barNum); } public static void main(String... arg) { Foo f = new Foo(); System.out.println("f.myBar.barNum is " + f.myBar.barNum); f.changeIt(f.myBar); System.out.println("f.myBar.barNum after changeIt is now " + f.myBar.barNum); } }
To conquer without peril, one ends up triumphing without glory
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
posted 2 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to CodeRanch!
Please tell us first what output you would have expected and why.
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Shadowing Object References?
shadowed variables
Shadowing of reference variables
scjp chapter3 2nd example of page 207-208
shadowing object reference variables
More...