aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected access??" Watch "Protected access??" New topic
Author

Protected access??

Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Hello,
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?
Thanks
-PC
-------------------------------------------------------
class B {
protected void method1(Object a) {
a = new Object();
//a="raj";
}
}

public class ATest extends B {
public static void main(String[] args){
B b=new B();
Object g = null;
b.method1(g);
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,
Corey


SCJP Tipline, etc.
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Thanks Corey, its clear now.
I appreciate your help
-PC
 
 
subject: Protected access??