Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Variable scope access question

Reply
Thread Tools

Variable scope access question

 
 
www
Guest
Posts: n/a
 
      06-21-2007
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.
 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      06-21-2007
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
 
Reply With Quote
 
 
 
 
Mark Rafn
Guest
Posts: n/a
 
      06-21-2007
>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/>
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-22-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.18 How can I access a dynamic variable while a similarly named lexical is in scope? PerlFAQ Server Perl Misc 0 03-27-2011 10:00 AM
FAQ 7.18 How can I access a dynamic variable while a similarly named lexical is in scope? PerlFAQ Server Perl Misc 0 01-21-2011 05:00 PM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
How to access application scope variable in Struts Action Class Nitin Java 1 07-27-2004 10:49 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57