473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Static variable and constructor

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.
Nov 16 '05 #1
10 2882
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.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
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.

Nov 16 '05 #2
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.
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:Il********************@news4.e.nsc.no...
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.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
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.


Nov 16 '05 #3
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++;

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:Il********************@news4.e.nsc.no...
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.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
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.



Nov 16 '05 #4
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'
Nov 16 '05 #5
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.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:Ok****************@TK2MSFTNGP12.phx.gbl...
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'

Nov 16 '05 #6
Rene <no****@nospam.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
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.
Nov 16 '05 #8
I do not in any way think it was a stupid question.
We all have to start from somewhere, do we not?

Take care.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #9
> 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
Nov 16 '05 #10
Rene <no****@nospam.nospam> wrote:
Thanks for putting up with my stupid question Dennis.


"The only stupid question is the one not asked."
-- Unknown
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.