wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes it's long but i can't help it. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "it Watch "it New topic
Author

it's long but i can't help it.

rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
public class Shadow {
public static void main(String s[]) {
S1 s1 = new S1();
S2 s2 = new S2();

System.out.println(s1.s); // prints S1
System.out.println(s1.getS()); // prints S1
System.out.println(s2.s); // prints S2
System.out.println(s2.getS()); // prints S2
s1 = s2;
System.out.println(s1.s); // prints S1, not S2 -
// since variable is resolved at compile time
System.out.println(s1.getS()); // prints S2 -
// since method is resolved at run time
}
}
class S1 {
public String s = "S1";

public String getS() {
return s;
}
}
class S2 extends S1{
//public String s = "S2";
}
here even after assigning variable s1 to s2 's1.s' still prints s1 and not s2.the reason for this is variables are resolved at compile time.
but in the following example,
after s2 being assigned to s1 it's value got changed
public class Shadow1 {
String s="rajini";
public static void main(String s[]) {
Shadow1 s1 = new Shadow1();
Shadow1 s2 = new Shadow1();
s1.s="s1";
s2.s="s2";
System.out.println(s1.s); // prints S1
System.out.println(s2.s); // prints S2
s1 = s2;
System.out.println(s1.s); // prints S1, not S2 -
// since variable is resolved at compile time
}
}

please clarify this doubt.iam very much confused.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Rajani,
It would help if you read the FAQ to learn how to format your code so it's more readable.
Thanks,
-Peter
Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
In the first case you are talking of inheritance - i.e. the variable in SuperClass and SubClass being the same - The variable to be accessed is determined by the Ref type.
in the second case they are two instances of the same class - so when one object is assigned to the other you are just giving the memory ref of the object i.e.
s1 = s2;
s1 is given the memory ref of s2, all the content of s1 is lost.
both are pointing to the same info -- hence it rightly points towards variables of s2.
Experts pls. comment!!!
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Rajani,
I don't understand your question. The output for your second example, class Shadow1 is:
s1
s2
s2
-Peter
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
my doubt is i know that references to member variables r computed at compile time using the type of the reference.
in my first program even after assigning variable s1 to s2 's1.s' still prints s1 and not s2.the reason for this is variables are resolved at compile time.
but in the second example,
after s2 being assigned to s1 ,s1.s prints out s2 and not s1.as variables r resolved at compile time this s1.s should print s1 i think.
hope u understand my problem
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
Rajani,
S1 ans S2 are two objects of the same class Shadow1 with s as an instance variable. S1 and S2 each point to a unique object initially. Now, you make s1 to point to the object of s2. s1.s gives the value of string s in the object of the class it is pointing to, which is here "s2".
Hope this helps.

public class Shadow1 {
String s="rajini";
public static void main(String s[]) {
Shadow1 s1 = new Shadow1();
Shadow1 s2 = new Shadow1();
s1.s="s1";
s2.s="s2";
System.out.println(s1.s); // prints S1
System.out.println(s2.s); // prints S2
s1 = s2;
System.out.println(s1.s); // prints S1, not S2 -
// since variable is resolved at compile time
}
}


Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: it's long but i can't help it.