Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18243

Re: Need help with a program.

From "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this>
Subject Re: Need help with a program.
Message-ID <50327C3D.57168.calajapr@time.synchro.net> (permalink)
Newsgroups comp.lang.java.programmer
References <50327C3D.57167.calajapr@time.synchro.net>
Date 2012-08-20 18:58 +0000
Organization tds.net

Show all headers | View raw


  To: yanamandra
From: Leif Roar Moldskred <leifm@dimnakorr.com>

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

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Need help with a program. "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
  Re: Need help with a program. "yanamandra" <yanamandra@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
    Re: Need help with a program. "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
      Re: Need help with a program. "yanamandra" <yanamandra@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
        Re: Need help with a program. "Leif Roar Moldskred" <leif.roar.moldskred@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
          Re: Need help with a program. "yanamandra" <yanamandra@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000
        Re: Need help with a program. "Lew" <lew@1:261/38.remove-fzq-this> - 2012-08-20 18:58 +0000

csiph-web