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
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
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
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
Bartender
Posts: 15737
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.
grapes are vegan food pellets. Eat this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
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...