473,396 Members | 2,037 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 or class

Hi All,

Would like to know which is the best approach. I have a class which has
only constants variables (private) and a public static method, which
returns a string variable.

I would like to know can I use it as static method, change this to a
class and construct it and use it every time. What is the best approach
and why one is better than other one? Any pointers would be great.

Thanks.

Jun 6 '06 #1
9 1353
On 6 Jun 2006 12:08:25 -0700, DBC User wrote:
Would like to know which is the best approach. I have a class which has
only constants variables (private) and a public static method, which
returns a string variable.

I would like to know can I use it as static method, change this to a
class and construct it and use it every time. What is the best approach
and why one is better than other one? Any pointers would be great.


I don't really understand what you're after. If your class only has
constant fields and a static method, then make its constructor private so
that it can not be instanciated (which wouldn't serve any purpose since it
doesn't have any member fields or methods) and there you go.
Jun 6 '06 #2
Hello DBC,

In general - use class if you are goint to use it from the different classes.
in that case you have only one instance and refer to it from the different
places

DU> Would like to know which is the best approach. I have a class which
DU> has only constants variables (private) and a public static method,
DU> which returns a string variable.
DU>
DU> I would like to know can I use it as static method, change this to a
DU> class and construct it and use it every time. What is the best
DU> approach and why one is better than other one? Any pointers would be
DU> great.
DU>
DU> Thanks.
DU>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 6 '06 #3

"DBC User" wrote...
Would like to know which is the best approach. I have a class which has
only constants variables (private) and a public static method, which
returns a string variable.

I would like to know can I use it as static method, change this to a
class and construct it and use it every time. What is the best approach
and why one is better than other one? Any pointers would be great.


Well, I guess you really ask the question you have in the subject: "static
class or class".

It's really no difference to use a static class or just a non-instatiable
class with a private constructor.

The benefits of making a class static, is to prevent to mistakingly adding
instance members to it if it's supposed to be just a holder for static
things. Then it will raise errors at compile time.

Otherwise, there's no difference.

/// Bjorn A
Jun 6 '06 #4
Bjorn,

Thanks so there will be no run time memory management issues I need to
worry about??

Thanks for all your answers.
Bjorn Abelli wrote:
"DBC User" wrote...
Would like to know which is the best approach. I have a class which has
only constants variables (private) and a public static method, which
returns a string variable.

I would like to know can I use it as static method, change this to a
class and construct it and use it every time. What is the best approach
and why one is better than other one? Any pointers would be great.


Well, I guess you really ask the question you have in the subject: "static
class or class".

It's really no difference to use a static class or just a non-instatiable
class with a private constructor.

The benefits of making a class static, is to prevent to mistakingly adding
instance members to it if it's supposed to be just a holder for static
things. Then it will raise errors at compile time.

Otherwise, there's no difference.

/// Bjorn A


Jun 6 '06 #5
Now, if I follow this correctly, you are asking which is better:

MyClassA.Func();

or

MyClassB obj = new MyClassB();
obj.Func();

In the second case, there is a trivial amount of overhead : creating a
MyClassB object which is empty except for a reference to MyClassB's
Type object, and then passing the "this" reference to the Func method.

DBC User wrote:
Thanks so there will be no run time memory management issues I need to
worry about??


Jun 6 '06 #6

"DBC User" wrote...
Bjorn,

Thanks so there will be no run time memory management issues
I need to worry about??
Not according to the documentation anyway... ;-)

http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx

"Creating a static class is therefore much the same as creating a class that
contains only static members and a private constructor. A private
constructor prevents the class from being instantiated.

The advantage of using a static class is that the compiler can check to make
sure that no instance members are accidentally added. The compiler will
guarantee that instances of this class cannot be created."
Thanks for all your answers.

You're welcome.

/// Bjorn A
Jun 6 '06 #7

<ja**********@gmail.com> wrote...
Now, if I follow this correctly, you are asking which is better:

MyClassA.Func();

or

MyClassB obj = new MyClassB();
obj.Func();

In the second case, there is a trivial amount of overhead : creating a
MyClassB object which is empty except for a reference to MyClassB's
Type object, and then passing the "this" reference to the Func method.


I think "DBC User" rather wondered about the difference between

class MyClassA
{
static void Func() {}
}

or

static class MyClassA
{
static void Func() {}
}

....which should be no difference at all.

The difference is only at compile time, where the compiler prohibits any
instance members in the second case.

In runtime, there would AFAIK be no difference, as it should produce the
exactly same IL.

// Bjorn A
Jun 6 '06 #8
Bjorn,

Sorry I wasn't clear enough.

This is what I am trying

public class foo
{
private string fooVar;
public foo()
{
fooVar = "Test";
}
public string GetStringFoo()
{
return fooVar;
}
}

and in my main code I use
foo f = new foo();
string t = f.GetStringFoo();

is it better than the following with respect to the run time

public class foo
{
private const string fooVar="Test";
public static string GetStringFoo()
{
return fooVar;
}
}
and in my main code I use
string t = foo.GetStringFoo();

I am more interested in which is the best approach and what is the
overhead I need to worry about.

Thanks a lot for all the answers again.

Bjorn Abelli wrote:
<ja**********@gmail.com> wrote...
Now, if I follow this correctly, you are asking which is better:

MyClassA.Func();

or

MyClassB obj = new MyClassB();
obj.Func();

In the second case, there is a trivial amount of overhead : creating a
MyClassB object which is empty except for a reference to MyClassB's
Type object, and then passing the "this" reference to the Func method.


I think "DBC User" rather wondered about the difference between

class MyClassA
{
static void Func() {}
}

or

static class MyClassA
{
static void Func() {}
}

...which should be no difference at all.

The difference is only at compile time, where the compiler prohibits any
instance members in the second case.

In runtime, there would AFAIK be no difference, as it should produce the
exactly same IL.

// Bjorn A


Jun 7 '06 #9

"DBC User" wrote...
This is what I am trying

public class foo
{
private string fooVar;
public foo()
{
fooVar = "Test";
}
public string GetStringFoo()
{
return fooVar;
}
}

and in my main code I use
foo f = new foo();
string t = f.GetStringFoo();

is it better than the following with respect to the run time

public class foo
{
private const string fooVar="Test";
public static string GetStringFoo()
{
return fooVar;
}
}
and in my main code I use
string t = foo.GetStringFoo();

I am more interested in which is the best approach
and what is the overhead I need to worry about.
The "best" approach is always relative... ;-)

The second case will gain "some" performance compared to the first case, as
the string as a constant can be inlined at compilation, i.e. it will not
need to be resolved at runtime.
Thanks a lot for all the answers again.


You're welcome, again... ;-)

/// Bjorn A
Jun 7 '06 #10

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...

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.