473,803 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

quick Static Method question

Hello,
I am new to C# and have a quick question.
Any replies much appreciated ;-)

It is regarding static methods...
Are they public by default?
I mean it seems like for them to be usable, they have to be public right?

EG. Below you can't call the method by saying Point.ObjectCou nt() unless
ObjectCount is public right? So this call would fail ?

class Point
{ ...
static int ObjectCount ()
{...
}
}

Thanks again-
Jeff
Nov 16 '05 #1
6 1223
z_learning_test er <so*****@micros oft.com> wrote:
I am new to C# and have a quick question.
Any replies much appreciated ;-)

It is regarding static methods...
Are they public by default?
No, it's private by default. The rule is that everything's as
restrictive as it can be by default.
I mean it seems like for them to be usable, they have to be public right?
Only if you need to call them from other assemblies. Sometimes you may
want static methods which are private, sometimes you may want internal
ones, and sometimes you may want public ones.
EG. Below you can't call the method by saying Point.ObjectCou nt() unless
ObjectCount is public right? So this call would fail ?

class Point
{ ...
static int ObjectCount ()
{...
}
}


Well, if you just declare "class Point" it'll be internal to the
assembly anyway, so making a method in it public isn't terribly useful
for the most part. As it stands above, you could only call ObjectCount
within the class itself.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Great! Thanks for the response.
I'm still struggling to see the point of having a private static method.
I mean you use static so you can access it outside the assembly without
creating an instance of the object right?

So instead of private static, why not just say private? Is there a
difference if its private?
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
z_learning_test er <so*****@micros oft.com> wrote:
I am new to C# and have a quick question.
Any replies much appreciated ;-)

It is regarding static methods...
Are they public by default?
No, it's private by default. The rule is that everything's as
restrictive as it can be by default.
I mean it seems like for them to be usable, they have to be public

right?
Only if you need to call them from other assemblies. Sometimes you may
want static methods which are private, sometimes you may want internal
ones, and sometimes you may want public ones.
EG. Below you can't call the method by saying Point.ObjectCou nt() unless
ObjectCount is public right? So this call would fail ?

class Point
{ ...
static int ObjectCount ()
{...
}
}


Well, if you just declare "class Point" it'll be internal to the
assembly anyway, so making a method in it public isn't terribly useful
for the most part. As it stands above, you could only call ObjectCount
within the class itself.

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

Nov 16 '05 #3
z_learning_test er <so*****@micros oft.com> wrote:
Great! Thanks for the response.
I'm still struggling to see the point of having a private static method.
To call it from within the same class, but not allow people to call it
from outside the class.
I mean you use static so you can access it outside the assembly without
creating an instance of the object right?
No. You use static so you can access it without creating an instance of
the object. It has nothing to do with accessibility.
So instead of private static, why not just say private? Is there a
difference if its private?


Static/instance and accessibility are entirely separate concepts.
Static/instance determines whether it operates on an instance or
whether it sort of applies in a type-wide way. Accessibility says where
you can call it from.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hmm, very interesting.
So if static static just determines how you call the method (by not having
to create an instance of the object to call it on, and rather to call it
directly on the class) then it would seem preferable to use static whenever
possible. No instance of the object = less memory used. True? After all it
seems just as easy to code it either way...

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
z_learning_test er <so*****@micros oft.com> wrote:
Great! Thanks for the response.
I'm still struggling to see the point of having a private static method.


To call it from within the same class, but not allow people to call it
from outside the class.
I mean you use static so you can access it outside the assembly without
creating an instance of the object right?


No. You use static so you can access it without creating an instance of
the object. It has nothing to do with accessibility.
So instead of private static, why not just say private? Is there a
difference if its private?


Static/instance and accessibility are entirely separate concepts.
Static/instance determines whether it operates on an instance or
whether it sort of applies in a type-wide way. Accessibility says where
you can call it from.

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

Nov 16 '05 #5
z_learning_test er <so*****@micros oft.com> wrote:
Hmm, very interesting.
So if static static just determines how you call the method (by not having
to create an instance of the object to call it on, and rather to call it
directly on the class) then it would seem preferable to use static whenever
possible. No instance of the object = less memory used. True? After all it
seems just as easy to code it either way...


You shouldn't think of it that way. Think of whether it's *naturally*
an instance method or not - does it operate on a particular instance,
or is it general, either only operating on static variables, or not
requiring any context at all.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Wow, good insight.
I wish my book explained it that way...
But then it is a beginner book and more focussed on the syntax than when and
why to use it.

Thanks again!

Jeff

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
z_learning_test er <so*****@micros oft.com> wrote:
Hmm, very interesting.
So if static static just determines how you call the method (by not having to create an instance of the object to call it on, and rather to call it
directly on the class) then it would seem preferable to use static whenever possible. No instance of the object = less memory used. True? After all it seems just as easy to code it either way...


You shouldn't think of it that way. Think of whether it's *naturally*
an instance method or not - does it operate on a particular instance,
or is it general, either only operating on static variables, or not
requiring any context at all.

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

Nov 16 '05 #7

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

Similar topics

6
3593
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
5
6295
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is JDK 6 downloadable from http://java.sun.com/javase/downloads/index.jsp. I will be using JDK 5(update 8)
0
9566
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.