473,396 Members | 1,738 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,396 software developers and data experts.

static class

Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain only
static members.

Thanks.
Nov 13 '05 #1
8 8882
You can't make a class static. Just make a regular class and don't
instantiate it.

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain only static members.

Thanks.

Nov 13 '05 #2
Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Math()' is inaccessible due to its protection level

But of course I can do this:
double d = Math.PI;

That's the type of functionality I want.

Thanks.

"Alien" <al***@sympatico.ca> wrote in message
news:ZK********************@news04.bloor.is.net.ca ble.rogers.com...
You can't make a class static. Just make a regular class and don't
instantiate it.

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain

only
static members.

Thanks.


Nov 13 '05 #3
I'm pretty new to C# so please someone correct me if I'm wrong but, if I
understand correctly, you can use double d = Math.PI; because Math makes
available a public static property, method, field, etc titled PI (or
whatever). Create your class as normal and create your
methods/properties/fields as static.

Nov 13 '05 #4
> Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Math()' is inaccessible due to its protection level Make constructor private (if you don't want to instantiate objects of your
class, you don't realy nead constructor, but if you do so, you will get
warning if you try Myclass m = new Myclass())
But of course I can do this:
double d = Math.PI; Make all members static

That's the type of functionality I want.

You will get what you want ;)))
Greetings
Nov 13 '05 #5
Someone,
Make the default constructor of your class private, so no one can create an
instance of the class.

Make the class itself sealed, so no one can derive from it.

Only create static members in the class.

Hope this helps
Jay
"someone" <a@a.com> wrote in message
news:R_*********************@news2.telusplanet.net ...
Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Math()' is inaccessible due to its protection level

But of course I can do this:
double d = Math.PI;

That's the type of functionality I want.

Thanks.

"Alien" <al***@sympatico.ca> wrote in message
news:ZK********************@news04.bloor.is.net.ca ble.rogers.com...
You can't make a class static. Just make a regular class and don't
instantiate it.

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain

only
static members.

Thanks.



Nov 13 '05 #6
Hi,

You CANNOT make a class static, you can make methods, properties and
variables of that class static , though.

If you don't want an instance of that classes to be created you can declare
the constructor as private therefore nobody can create it. Also remember
that if you are going to have only static method then this constructor will
never be called, therefore you will need to have a constructor marked as
static

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain only static members.

Thanks.

Nov 13 '05 #7
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote:
You CANNOT make a class static, you can make methods, properties and
variables of that class static , though.
Agreed...
If you don't want an instance of that classes to be created you can declare
the constructor as private therefore nobody can create it.
Agreed.
Also remember
that if you are going to have only static method then this constructor will
never be called, therefore you will need to have a constructor marked as
static


Not agreed. You *may* need a static constructor, but in many cases you
don't.

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

Also remember
that if you are going to have only static method then this constructor will never be called, therefore you will need to have a constructor marked as
static


Not agreed. You *may* need a static constructor, but in many cases you
don't.


Ok, you are right, the correct word is "may" and no "need" , the english is
not my first language and sometimes I mix the words :)
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 13 '05 #9

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

Similar topics

2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
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...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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,...

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.