Static variable and constructor

R

Rene

I jus realized that I can change the values of "static variables" and
"instance variable" through the standard constructor This means that
something like this will compile:

public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable
xyz++; // Static Variable
}
int abc;
static int xyz;
}

However, if instead of using the standard constructor I use a static
constructor, I can only initialize the static variables. The thing that gets
me, is that I always looked at static members on a world of their own
totally separated from the instance class world. But now it looks like the
static variable is going transsexual on me.

Am I missing something? Am I the only one that thinks this behavior is kind
of breaking the rules?
Thanks.
 
D

Dennis Myrén

Of course you can access the static context from the instance context.
If something is declared static, it does not in any way mean it can only be
accessed from
a static context. If it was, then a fundamental purpose of static members
have been lost.

From a static context however, you cannot access instance members.
Actually, that is very logical. Which instance would we otherwise refer to?
Yes, you can write xyz++. However, you cannot write this.xyz++, but you can
write
this.abc++, since it is an instance member, and thus is in context of
'this', where xyz is not.
When you are changing the abc variable, the value is changed in the scope of
that particular instance.
When you are changing the xyz variable, the value is changed in a global
scope.

Yes, you are right, static members are totally separated from the instance
members.
But you can still reach them from anywhere. Otherwise, there would be just
no point in having static members.

Hope i have been for help in this question.
 
R

Rene

I guess it would make more sense to me if the required syntax was something
like:

SomeClass. xyz++;

not just the plain xyz++, just calling xyz++ does not seem like your typical
static call.
 
D

Dennis Myrén

Well, specifying the class in this case is superfluos, since you are already
there.
If, from another class, you would like to change xyz, then yes you will of
course
need to use the fully qualified name, which is [NAMESPACE(S)].SomeClass.
xyz++;
 
R

Rene

And since I am in a role asking stupid questions why is it that you can't
have an instance and static variables declare with the same name? Aren't
both variables in totally different scopes?

int abc;
static int abc; // Error: already contains a definition for 'abc'
 
D

Dennis Myrén

If it was possible to declare an instance member and a static member
with the same name, in the same class, then you would always have to
refer to the static member with it's fully qualified name, or refer to the
instance member with "this" always, to distuingish them.
public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable(or is it the static?)
abc++; // Static Variable(or is it the instance member?)
this.abc++//Certainly the instance member.
SomeClass.abc++//Certainly the static member.
}
int abc;
static int abc;
}

This is a C# design question, which i am not in position to answer.
My best bet though, why this is not possible, is that it would be kind of
nasty.
This is a limitation of C# though, as it would be possible to do in the
Intermediate Language.
 
J

Jon Skeet [C# MVP]

Rene said:
And since I am in a role asking stupid questions why is it that you can't
have an instance and static variables declare with the same name? Aren't
both variables in totally different scopes?

int abc;
static int abc; // Error: already contains a definition for 'abc'

No, they have the same scope. From the C# spec:

<quote>
The scope of a member declared by a class-member-declaration (§17.2) is
the class-body in which the declaration occurs. In addition, the scope
of a class member extends to the class-body of those derived classes
that are included in the accessibility domain (§10.5.2) of the member.
</quote>

class-member-declaration includes both instance variables and static
variables.

I think the main reason for disallowing it is to try to prevent
confusion. In theory you could distinguish between them using this.abc
or ClassName.abc, but I don't think it would ever really be a good
idea.
 
R

Rene

Thanks for putting up with my stupid question Dennis. I am still learning
and every once in a while a connection goes bad in my brain and I get
confused with the stupidest most basic things.



Thanks again.
 
D

Dennis Myrén

I do not in any way think it was a stupid question.
We all have to start from somewhere, do we not?

Take care.
 
R

Richard Blewett

From a static context however, you cannot access instance members.
Actually, that is very logical. Which instance would we otherwise refer
to?
Yes, you can write xyz++. However, you cannot write this.xyz++, but you
can write
this.abc++, since it is an instance member, and thus is in context of
'this', where xyz is not.
When you are changing the abc variable, the value is changed in the scope
of that particular instance.
When you are changing the xyz variable, the value is changed in a global
scope.

One small nit, you can access instance state from a static method as long as
that method is passed an instance:

class Foo
{
int abc;
public static Bar(Foo f)
{
f.abc++;
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 

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