Variable scope access question

Discussion in 'Java' started by www, Jun 21, 2007.

  1. www

    www Guest

    Hi,

    I am not sure my question is valid or not. It is the following:


    public class MyClass {

    public void doA() {
    int num = 10;

    doB();
    //Now, num value has been changed
    }

    public void doB() {
    //I need to access and change the value num inside doA. But I don't
    know how to do it.


    }
    }

    Is this possible? Thank you for your help.
     
    www, Jun 21, 2007
    #1
    1. Advertising

  2. In article <f5eg90$pkq$>, www <>
    wrote:

    > Hi,
    >
    > I am not sure my question is valid or not. It is the following:
    >
    >
    > public class MyClass {
    >
    > public void doA() {
    > int num = 10;
    >
    > doB();
    > //Now, num value has been changed
    > }
    >
    > public void doB() {
    > //I need to access and change the value num inside doA. But I don't
    > know how to do it.
    >
    >
    > }
    > }
    >
    > Is this possible? Thank you for your help.


    In your example, the variable "num" is local to the method named "doA"
    and is therefore not accessible to *any* code outside that method.
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Jun 21, 2007
    #2
    1. Advertising

  3. www

    Mark Rafn Guest

    >I am not sure my question is valid or not. It is the following:
    >public class MyClass {
    > public void doA() {
    > int num = 10;
    > doB();
    > //Now, num value has been changed
    > }
    > public void doB() {
    > //I need to access and change the value num inside doA. But I don't
    > //know how to do it.
    > }
    >}


    This isn't possible. It's also very much against the grain of structured
    programming - doB can not know that it's called only from within doA, so it
    can't access locals that only exist in doA.

    Find another way to design your class such that scope of data elements is
    cleaner.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Jun 21, 2007
    #3
  4. www

    Lew Guest

    www wrote:
    >> I am not sure my question is valid or not. It is the following:
    >> public class MyClass {
    >> public void doA() {
    >> int num = 10;
    >> doB();
    >> //Now, num value has been changed
    >> }
    >> public void doB() {
    >> //I need to access and change the value num inside doA. But I don't
    >> //know how to do it.
    >> }
    >> }


    Please do not embed TABs in Usenet posts.

    Mark Rafn wrote:
    > This isn't possible. It's also very much against the grain of structured
    > programming - doB can not know that it's called only from within doA, so it
    > can't access locals that only exist in doA.
    >
    > Find another way to design your class such that scope of data elements is
    > cleaner.


    As with so many programming problems, one can redefine the problem to achieve
    the result.

    Instead of an int, define a holder:

    public class MyClass
    {
    static class Holder
    {
    public int num;
    }
    public void doA()
    {
    Holder h = new Holder();
    h.num = 17;
    doB( h );
    }
    public void doB( Holder hold )
    {
    hold.num *= 2;
    }
    }

    Of course, within a single class this makes little sense. The usual approach
    there is to use an instance variable. But for creating an OUT variable
    between objects of different types the holder idiom works well.

    --
    Lew
     
    Lew, Jun 22, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin
    Replies:
    1
    Views:
    8,739
    Murray
    Jul 27, 2004
  2. David Filmer
    Replies:
    19
    Views:
    281
    Kevin Collins
    May 21, 2004
  3. PerlFAQ Server
    Replies:
    0
    Views:
    151
    PerlFAQ Server
    Jan 21, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    146
    PerlFAQ Server
    Mar 27, 2011
  5. Andrew Falanga
    Replies:
    2
    Views:
    217
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page