Basic Variable Scope Question

A

arcticool

Fields vs. Local Variables- as I understand a Field (declared inside
a class) can share data across methods in the class, and to other
classis (if declared public) where local variables (declared inside
a method) are scoped only within the method.

So my question is: what would be the point of declaring a local
variable *public* ?
Thanks,

AC
--
 
J

Jason Hales

It's an oo design concept -it's considered bad design to expose
variables directly by marking them public as you have no control on how
they are set by users of your class.

It's particularly important if you need to validate ranges of values
passed in or if you want values to be write-once or to not allwo null
values etc.

Alot of people don't use 'wrapping' properties out of laziness I guess
- but inly takes a few minuntes to declare and use
 
B

Brian Gideon

AC,

You can't declare a local variable as public. The compiler generates
an error. Maybe I don't understand your question.

Brian
 
B

Bruce Wood

it's considered bad design to expose variables directly by marking them public...

Considering the OP's question, please be more careful with your choice
of words. :)

It's considered bad design to expose *fields* directly by marking them
public.

You can't expose a variable. Its scope is local to a method and that's
that.
 
A

arcticool

Thanks for the response, any help I can get at this point is much
appreciated.
--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top