Basic Variable Scope Question

  • Thread starter Thread starter arcticool
  • Start date Start date
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
--
 
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
 
AC,

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

Brian
 
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.
 
Back
Top