From: Wayne on
On 6/23/2010 4:17 AM, Mike Barnard wrote:
> ...
> But this makes me ask a question, that I'll experiment with tonight I
> hope.
>
> Q. If I declare such a class-level static variable *within a method*,
> bearing in mind the Sun tutorial statement that "...local variables
> are only visible to the methods in which they are declared; they are
> not accessible from the rest of the class." will that limit what other
> code can access the static variable? Will I only be able to see it
> from inside the method?

You can't declare class-level variables within a method. Only local
variables. And you can't declare local variables as "static". The
designers of Java felt there was little benefit to adding such complexity
to the language, so they didn't.

Which also makes me ask:
>
> Q. What is the convention for creating such a variable? Place it in
> the class, outside methods?

That's exactly right. If you need to store some data that will be used
by several methods, declare it outside of any method (but still inside
the class; you can't declare variables outside of a class). In reality
the question doesn't come up as often as you might think. Usually you
declare variables inside of methods, and when one method must invoke
another, it can "pass" the data as an argument in the method call.

The trick to thinking about this is to concentrate on the methods: the
behaviors or tasks that your objects can do. Then any data unique to
an object, that must be remembered between method calls should be declared
as an instance variable, that is, outside of any method but without the
static keyword. (For example, the "Name" of each Person object.) A class
variable is best used to hold data not associated with any particular
object, but rather the class as a whole. (For example, the number of
people---that is, the number of Person objects---could be kept in a
class variable.)

>
> As I said, I'll experiment. And read more!
>
> Thank you.
>
> Mike.

Hope this helps a bit.

--
Wayne
From: Lew on
Mike Barnard wrote:
> Q. If I declare such a class-level static variable *within a method*,

You can't.

> bearing in mind the Sun tutorial statement that "...local variables
> are only visible to the methods in which they are declared; they are
> not accessible from the rest of the class." will that limit what other
> code can access the static variable? Will I only be able to see it
> from inside the method? Which also makes me ask:

There is no such thing as a static variable declared within a method.

> Q. What is the convention for creating such a variable? Place it in
> the class, outside methods?

You can't.

Variables declared within a method are local, never static or instance.

--
Lew
From: Mike Barnard on
On Wed, 23 Jun 2010 09:17:10 +0100, Mike Barnard
<m.barnard.trousers(a)thunderin.co.uk> wrote:

To both Lew and Wayne, thanks for the replies. Hopefully I'll get more
time tonight and play. I'll put a static variable in a method and see
what the error is, etc.


From: Lew on
Lew wrote:
>     public void instanceLevel()
>     {
>        String belongaLocal = "This method cannot use 'belongaInstance'";
>

That String lies. It should have been:

String belongaLocal = "This method *can* use 'belongaInstance'";

Sorry.

--
Lew
First  |  Prev  | 
Pages: 1 2
Prev: Beans binding + disappointment
Next: PreparedStatement