• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajani,
It would help if you read the FAQ to learn how to format your code so it's more readable.
Thanks,
-Peter
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajani,
I don't understand your question. The output for your second example, class Shadow1 is:
s1
s2
s2
-Peter
 
rajani adapa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic