private static

T

TomislaW

What is the purpose or difference between
private static and private method in non-static class?
 
K

Kevin Spencer

If I understand you correctly, you want to know the difference between a
private static method in a non-static class, and a private non-static method
in a non-static class? If I am correct in my understanding of your
question...

A non-static class is a class which requires instantiation to use. A
non-static member of a class is a member which requires an instantiation of
its host class to use. A static member of a class is a member which does NOT
require an instantiation of its host class to use.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
K

Karl Seguin

Kevin's right and to build on it...a public static method in an instance
class often provides utility functionality to the outside which somehow
relates to the class. For example, given a User class, you might have a
public static method to say, get a user class by user id:

public class User{
private int userId...

public static User GetUserById(int userId){
}
}

A private static method in an instance class pretty much does the same, but
only exposes the functionality internally to the class. In my experience,
the need for this doesn't come up too often. I've probably used it the most
when I provider overloads of public statics and they all use the same
private static, that for some reason I don't want exposed.. Just my $0.02

Karl
 
D

Dan Bass

I think the confusion lies in that a static method, as you say, does not
require an instantiation of the host class, but being private, nothing can
access it but an instantiation of the host?!

Therefore, would it be right to suggest that there's never a reason for
using private and static together in declaring a function?
 
H

Hans Kesting

Dan said:
I think the confusion lies in that a static method, as you say, does
not require an instantiation of the host class, but being private,
nothing can access it but an instantiation of the host?!

Therefore, would it be right to suggest that there's never a reason
for using private and static together in declaring a function?

You can call private static methods from other static methods
(possibly public) in that same class.

Hans Kesting
 
D

Dan Bass

Hans,

For some reason I thought this wouldn't compile because the statics weren't
both public!
cheers for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top