aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes variable scope Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "variable scope" Watch "variable scope" New topic
Author

variable scope

praveen rao
Greenhorn

Joined: Mar 14, 2002
Posts: 3
class Q31{
public static void main(String args[]){
SubClass ref1 = new SubClass(5);
SubClass ref2 = new SubClass(10);
System.out.println(
ref1.add(ref2));
}//end main()
}//end class definition
class AClass{
protected int x;

AClass(int x){//constructor
this.x = x;
}// end constructor
}//end class AClass
class SubClass extends AClass{
SubClass(int x){
super(x);
}//end constructor

int add(AClass ref){
return x + ref.x; // 1
}//end add()

}//end class SubClass

The output of this program is 15.. I am not able to figure out at (1) the value of x is 5.. Should this not be 10 ...
when SubClass ref2 = new SubClass(10); executes will the value of x not change to 10 ???
Thanks
Praveen
[ March 22, 2002: Message edited by: praveen rao ]
Neelima Rao
Greenhorn

Joined: Feb 24, 2002
Posts: 26
Had x been declared static, then it would have become 10 as you mentioned, instead of 5.
Anil Rudraraju
Greenhorn

Joined: Mar 02, 2002
Posts: 7
hi praveen,
I didn't get what u meant by

when SubClass ref2 = new SubClass(10); executes will the value of x not change to 10 ???

looking at the code,what i understand is...
Here there r 2 Subclass variable references(ref1 and ref2), pointing to 2 Subclass objects, which have their own seperate x variable.
ref1.x = 5( new Subclass(5)) and
ref2.x = 10( new Subclass(10))
then when u do ref1.add(ref2)
code at // 1 will be
return x + ref.x (return 5 + 10)
so the result is 15. correct me if i'am wrong.
HTH
[ March 22, 2002: Message edited by: Anil Rudraraju ]
Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Praveen,
At the begginnig of the program you have to understand that you are creating two completely separate objects. Each one of them will have its own x as an instance variable. The object ref1 will have an x variable initialized to 5 by its constructor and object ref2 will also have its x variable but this time initialized at 10 by the constructor.
Later in the main method you call the add method from within the ref1 object and you pass to it the object ref2.
So the x value is seen by the add method as the same x as its own object, here ref1. In the ref1 object x is 5. then you add the ref.x value from the other object ref2 that was passed to the method and for that object ref2, x is equal to 10.
In other words in the add method you are adding x's that are coming from two different objects.
The one from ref1 and the one from ref2.
Does this explanation makes sense to you? I hope it helps.


<a href="http://www.ajmasters.com" target="_blank" rel="nofollow">http://www.ajmasters.com</a> Real Estate, Tampa Florida
praveen rao
Greenhorn

Joined: Mar 14, 2002
Posts: 3
Thanks to all who replied. Now its clear as how it works..
Thanks Mario it helped me a lot
Praveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable scope
 
Similar Threads
protected access specifier
Queries
Garbage collection question from J@Whiz1.4
how to get the answer as 15 for this code
Inheritance -- Protected Modifier