Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Variable scope access question

Thread view: 
www - 21 Jun 2007 19:33 GMT
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.
Steve W. Jackson - 21 Jun 2007 19:50 GMT
> Hi,
>
[quoted text clipped - 18 lines]
>
> 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.
Signature

Steve W. Jackson
Montgomery, Alabama

Mark Rafn - 21 Jun 2007 20:16 GMT
>I am not sure my question is valid or not. It is the following:
>public class MyClass {
[quoted text clipped - 8 lines]
>    }
>}

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    dagon@dagon.net    <http://www.dagon.net/>
Lew - 22 Jun 2007 02:04 GMT
www wrote:
>> I am not sure my question is valid or not. It is the following:
>> public class MyClass {
[quoted text clipped - 8 lines]
>>     }
>> }

Please do not embed TABs in Usenet posts.

> 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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.