It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected access?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected access??" Watch "Protected access??" New topic

Protected access??

Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
I've this code. I'm not able to figure out why in class ATest after oveeriding method1 I'm getting null? Shouldn't I get value of a from super class?
I'm lost. Can any body plz help me out?
class B {
protected void method1(Object a) {
a = new Object();

public class ATest extends B {
public static void main(String[] args){
B b=new B();
Object g = null;
System.out.println (g); // why it gives me null??
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I think you're a little confused about how parameters are being passed.
When you have the line:

You have now invoked the method method1 and sent a reference to the object referenced by g. If we draw a picture, we see that we have something like this:

Now, in method1, you execute this line:

As we're now assigning a new reference to a, we change our picture to reflect that. It now looks like this:

Notice that g is unchanged. Therefore, we end up with null being printed out.
I hope that helps,

SCJP Tipline, etc.
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Thanks Corey, its clear now.
I appreciate your help
I agree. Here's the link:
subject: Protected access??
It's not a secret anymore!