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

public static class

Hello!

I have a class where all the members are static so I change the class to be
static but I have
run into one small problem and that is how do I code to prevent calling the
C-tor because
there is no point in creating an instance because all the members are
static.

For example I can't code private class XX because the class cannnot have
instance C-tor.

//Tony

Oct 1 '08 #1
4 1597
On Oct 1, 8:34*am, "Tony Johansson" <t.johans...@logica.comwrote:
I have a class where all the members are static so I change the class to be
static but I have run into one small problem and that is how do I code to
prevent calling the C-tor because
there is no point in creating an instance because all the members are
static.

For example I can't code private class XX because the class cannnot have
instance C-tor.
Static classes don't have constructors at all. The compiler doesn't
emit a default parameterless constructor for you. That's one of the
benefits of static classes.

Jon
Oct 1 '08 #2
On Wed, 01 Oct 2008 00:34:19 -0700, Tony Johansson
<t.*********@logica.comwrote:
Hello!

I have a class where all the members are static so I change the class to
be static but I have
run into one small problem and that is how do I code to prevent calling
the C-tor because
there is no point in creating an instance because all the members are
static.

For example I can't code private class XX because the class cannnot have
instance C-tor.
Right. But you also should not be able to instantiate a static class.

Do you have a code sample that violates that rule? That is, it allows you
to "new" an instance of a static class? The compiler should be preventing
it from happening for you. If it doesn't, that would be a bug.

Pete
Oct 1 '08 #3
Peter Duniho wrote:
On Wed, 01 Oct 2008 00:34:19 -0700, Tony Johansson
<t.*********@logica.comwrote:
>Hello!

I have a class where all the members are static so I change the class
to be static but I have
run into one small problem and that is how do I code to prevent
calling the C-tor because
there is no point in creating an instance because all the members are
static.

For example I can't code private class XX because the class cannnot
have instance C-tor.

Right. But you also should not be able to instantiate a static class.

Do you have a code sample that violates that rule? That is, it allows
you to "new" an instance of a static class? The compiler should be
preventing it from happening for you. If it doesn't, that would be a bug.

Pete

Can you add the "Abstract" to the class?

I think you can add "static", that means there will be no any derived
class from this class.
Add "Abstract" to prevent any instance of the class and you can still
call any static function of the class.

--
Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
Oct 2 '08 #4
On Wed, 01 Oct 2008 19:23:28 -0700, jacky kwok <ja***@nospam.nospamwrote:
Can you add the "Abstract" to the class?
No, that would make no sense.
I think you can add "static", that means there will be no any derived
class from this class.
No, static means that there are no instance members in the class.
Add "Abstract" to prevent any instance of the class and you can still
call any static function of the class.
A static class already cannot be instantiated. There's no need to add
anything else, even if you could add "abstract", which you can't.

Pete
Oct 2 '08 #5

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

Similar topics

13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
6
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...
5
by: Jassim Rahma | last post by:
Hi, I have a problem with the public class. I am trying to save the last identifier which I am getting correctrly but when I try to read I am getting 0. The public class is like this: ...
5
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
5
by: pali | last post by:
Hi, I was just wondering, why is not possible to make a member of a class BOTH abstract and static? MSDN says just that's in an error, not why this is so. In a nutshell, I need this: I have an...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
5
by: selimsivad83 | last post by:
Hi everyone, Thinking in Java, 4th edition, p 232: ... if you don't put an access specifier for class access, it defaults to package access. ... However, if a static member of that class is...
17
by: Gerrit | last post by:
Hallo, In my application, the user must logon with a username and a password. In all the tables in my database, there is a field user. By saving new or changed records, the username is writed to...
15
by: archana | last post by:
Hi all, can anyone tell me differene between public static and private static method. how they are allocated and access?. thanks in advance.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.