Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18173
| From | Leif Roar Moldskred <leifm@dimnakorr.com> |
|---|---|
| Subject | Re: Need help with a program. |
| Newsgroups | comp.lang.java.programmer |
| References | <82bcd41c-df92-4103-b674-718cf64a999b@x8g2000yqf.googlegroups.com> <Rp-dnfT58e_Vm6_NnZ2dnUVZ8kydnZ2d@giganews.com> <8fc96fef-1eb1-41bb-a5d2-619ce71f5101@googlegroups.com> <ALednd5HHvkJk6_NnZ2dnUVZ8iadnZ2d@giganews.com> <ba00e484-489c-4b6b-af25-f1f6a0ebca54@googlegroups.com> |
| Message-ID | <gMidneib4fYUvq_NnZ2dnUVZ8lmdnZ2d@giganews.com> (permalink) |
| Date | 2012-08-20 06:46 -0500 |
yanamandra <venu.yanamandra@gmail.com> wrote:
>
> Yes, as per the output, it looks like that. But, where can I confirm
> that all three are different variables? Can I print addresses of the
> 3 variables, or is there any utility in java that can print the
> output similar to "nm"?
I'm afraid there's no way to get hold of the address or reference to a
primitive type in Java. To confirm that these are different variables
you have to settle for the fact that they contain different values.
Consider the following (might contain typos):
public class Base {
private int i = 0;
public int getBaseI( ) {
return i;
}
public void setBaseI( int value ) {
i = value;
}
}
public class Extended extends Base {
private int i = 0;
public int getExtendedI( ) {
return i;
}
public void setExtendedI( int value ) {
i = value;
}
}
public class Test {
public static void main( String[] args ) {
Extended obj = new Extended( );
obj.setBaseI( 5 );
obj.setExtendedI( 10 );
System.out.println( "Base: " + obj.getBaseI() + " Extended: " + obj.getExtendedI() );
}
}
>
> Also, for the output I need, 40, 50 and 60, I will have to re-write the printall() function. Is that the only way?
As long as the two i2 variables are private, the only way to solve it
is to override the printall() method in the extended class. A better
way to do it is to have the printall() method refer to public or
protected getter method and then override the getter method:
public class Base {
private int i = 10;
protected int getI( ) {
return i;
}
public String toString() {
return "i = " + getI( );
}
}
public class Extended extends Base {
private int i = 20;
protected int getI( ) {
return i;
}
}
public class Test {
public static void main( String[] args ) {
Base base = new Base( );
Extended ext = new Extended( );
System.out.println( "Base: " + base );
System.out.println( "Extended: " + ext );
}
}
--
Leif Roar Moldskred
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 02:24 -0700
Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-20 04:41 -0500
Re: Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 02:57 -0700
Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-20 05:16 -0500
Re: Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 03:39 -0700
Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-20 06:46 -0500
Re: Need help with a program. yanamandra <venu.yanamandra@gmail.com> - 2012-08-20 04:58 -0700
Re: Need help with a program. Lew <noone@lewscanon.com> - 2012-08-20 07:31 -0700
Re: Need help with a program. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-08-21 03:43 -0500
Re: Need help with a program. Roedy Green <see_website@mindprod.com.invalid> - 2012-08-20 17:22 -0700
csiph-web