473,405 Members | 2,282 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,405 software developers and data experts.

Field vs Static Field, basic question...

I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.

My book sux and doesn't explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the
class can use it.
And a static field is described in my book as the same?

I must be missing something...


Nov 16 '05 #1
12 1686
Learning C# <so*****@microsoft.com> wrote:
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.

My book sux and doesn't explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the
class can use it.
And a static field is described in my book as the same?

I must be missing something...


It's the same difference as between a static method and an instance
method, and a static property and an instance property.

Anything "static" is to do with the type in general, not any particular
instance of the type. (You might use a static field to count how many
instances have been created, for example.)

Anything non-static is specific to that particular instance. (You might
use an instance field to say, "This instance was the <n>th one to be
created, for example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

"Learning C#" <so*****@microsoft.com> wrote in message
news:Ze********************@comcast.com...
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.
My book sux and doesn't explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the class can use it.
And a static field is described in my book as the same?

I must be missing something...


A field (from what I understand is a property without a property declaration
(public variable)).
Apart from that everything that applies to properties applies to fields
(static vs instance etc...)

ie
public class x
{
private int m_x; //private var x
public int y; //field var y
private static int m_X; //private static var X
public static int Y; //static field Y

public int x //instance property
{
set
{
return m_x;
}
get
{
m_x = value;
}

public static int X //static property
{
get
{
return m_X;
}
set
{
m_X = value;
}
}
Nov 16 '05 #3
John Baro <jo***@NOSPAMmesware.com.au> wrote:
A field (from what I understand is a property without a property declaration
(public variable)).
No. A field is a variable. A property doesn't have to have a variable
backing it at all.
Apart from that everything that applies to properties applies to fields
(static vs instance etc...)


Well, lots of things, but not everything. For instance, a property
can't be passed by reference.

A property is essentially a method (or pair of methods) and a bit of
metadata to allow those methods to be accessed using field-like syntax.

But yes, the meaning of "static" is the same for all member types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Thanks for the reply.
Just so I am clear, so they both are used to track the number of instances?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Learning C# <so*****@microsoft.com> wrote:
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.
My book sux and doesn't explain this well. It seems that a field is scoped the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the class can use it.
And a static field is described in my book as the same?

I must be missing something...


It's the same difference as between a static method and an instance
method, and a static property and an instance property.

Anything "static" is to do with the type in general, not any particular
instance of the type. (You might use a static field to count how many
instances have been created, for example.)

Anything non-static is specific to that particular instance. (You might
use an instance field to say, "This instance was the <n>th one to be
created, for example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
That's the beauty of programing, you can do almost everything you want
:)

The idea behind Jon's explanation is other:
public class MyClass
{
public int inst = 1;
public static int sta = 1;

public int GetSum()
{
return inst + sta;
}
}

....

MyClass a = new MyClass();
MyClass b = new MyClass();

a.inst = 5;
b.inst = 6;
MyClass.sta = 7;

now:
a.GetSum will return 12, and b.GetSum will return 13.

So, the sta field is one and the same for all objects of that class,
while inst is different per instance.
Sunny
In article <Zc********************@comcast.com>, so*****@microsoft.com
says...
Thanks for the reply.
Just so I am clear, so they both are used to track the number of instances?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Learning C# <so*****@microsoft.com> wrote:
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.
My book sux and doesn't explain this well. It seems that a field is scoped the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the class can use it.
And a static field is described in my book as the same?

I must be missing something...


It's the same difference as between a static method and an instance
method, and a static property and an instance property.

Anything "static" is to do with the type in general, not any particular
instance of the type. (You might use a static field to count how many
instances have been created, for example.)

Anything non-static is specific to that particular instance. (You might
use an instance field to say, "This instance was the <n>th one to be
created, for example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #6
Learning C# <so*****@microsoft.com> wrote:
Thanks for the reply.
Just so I am clear, so they both are used to track the number of
instances?


Not at all - that was just an example which demonstrated something
general to the whole type (namely the number of instances generated in
total) and something specific to each instance (the "instance number").

--
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
Sunny <su******@icebergwireless.com> wrote:
So, the sta field is one and the same for all objects of that class,
while inst is different per instance.


Not quite - there *is* no "sta" field for "all objects of that class" -
there is just the "sta" field for the class itself, rather than for
*any* instance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Yes, you are right.

In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...
Sunny <su******@icebergwireless.com> wrote:
So, the sta field is one and the same for all objects of that class,
while inst is different per instance.


Not quite - there *is* no "sta" field for "all objects of that class" -
there is just the "sta" field for the class itself, rather than for
*any* instance.

Nov 16 '05 #9
This may help...
http://www.geocities.com/jeff_louie/OOP/oop4.htm
Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
John Baro <jo***@NOSPAMmesware.com.au> wrote:
A field (from what I understand is a property without a property declaration (public variable)).
No. A field is a variable.
A property doesn't have to have a variable backing it at all.

Agreed (Forgot to mention :)
Apart from that everything that applies to properties applies to fields
(static vs instance etc...)


Well, lots of things, but not everything. For instance, a property
can't be passed by reference.

Correct.
Can you pass a "field" by ref then?
ie
MyMethod(ref MyClassInstance.MyField)

MyMethod(ref MyClassInstance.MyProperty) //will not work

(Goes away and tests)
Yes, it appears you can.
Learn something new every day.
A property is essentially a method (or pair of methods) and a bit of
metadata to allow those methods to be accessed using field-like syntax.
But "appears" from the outside to be the same as a field.
But yes, the meaning of "static" is the same for all member types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #11
John Baro <jo***@NOSPAMmesware.com.au> wrote:
A property is essentially a method (or pair of methods) and a bit of
metadata to allow those methods to be accessed using field-like syntax.


But "appears" from the outside to be the same as a field.


Well, it has (as I said) field-like syntax. That's the only similarity
with a field. You can still tell (from the outside) that it's a
property in any number of ways, including reflection.

It is my belief that everyone should know when they're changing a field
(which should be extremely rare outside the class itself) and when
they're changing a property, and that properties are more like methods
in all but syntax than they are like fields.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
The answer to your question has been covered, but a caveat to a static field
is that you will want to be very careful in how you expose that field so
that you don't have threading problems with other instances of the class
accessing the variable.

"Learning C#" <so*****@microsoft.com> wrote in message
news:Ze********************@comcast.com...
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.
My book sux and doesn't explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the class can use it.
And a static field is described in my book as the same?

I must be missing something...


Nov 16 '05 #13

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

Similar topics

4
by: gorda | last post by:
Hello, I have the following code, with a basic class having a constructor and destructor. in the main section, I create a static pointer to the class. My question is regarding the destructor. If...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
3
by: Alexander | last post by:
Hi all I has Component inside ASP.NET page that create CO object and store reference to it in the Component's privat static field. Then I cast this reference to desired interfac and call...
4
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the app-domains unloaded is the question. I have read...
74
by: Mark | last post by:
Hello Friends Please check the code below. One in C# and other in VB .Net In C# I am not able to access a static property by an instance variable, but in VB I can do it easily. The Error is ...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
2
by: anuragch1 | last post by:
#include <stdio.h> void main() { static int i = 1; void cal(); printf("%d\n",i); cal(); } void cal()
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.