views:

9202

answers:

13

All my college years I have been using public, and would like to know the difference between public, private, and protected? Also what does static do as opposed to having nothing?

+4  A: 

Most of that is answered on this page: http://msdn.microsoft.com/en-us/library/ms173121.aspx

IainMH
I think by "nothing" he meant what is used if you don't specify any access modifiers.
Joel Coehoorn
Nothing is a bit different than null. Null is a reference type specific item. Nothing works with both reference types and value types. It simply means go to 0 state (literally all 0s)
JaredPar
LOL. Yes, I think you're right.
IainMH
@JaredPar - Cool - I didn't know that.
IainMH
+1  A: 

Static explained here: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

Alex Reitbort
+1  A: 

Those access modifiers specify where your members are visible. You should probably read this up. Take the link given by IainMH as a starting point.

Static members are one per class and not one per instance.

Smasher
A: 

mmm...

Static means that you can access that function without having an instance of the class.

You can access directly from the class definition.

gbianchi
+2  A: 

Hmm.

See here: Access Modifiers.

In a nutshell:

Public gives the method or type complete visibility from other types/classes.

Private allows only the type containing the private method/variable access to the private method/variable (note that nested classes also have access to the containing classes private methods/variables).

Protected is similar to private except derived classes can also access protected methods.

"Nothing" is VB.NET's equivalent to null. Although if you're referring to "nothing" meaning "no access modifier", then it depends, although a very rough rule of thumb (certainly in C#) is that if you don't explicitly specify an access modifier, the method/variable declaration is usually as restricted as it can be. i.e.

public class MyClass
{
    string s = "";
}

is effectively the same as:

public class MyClass
{
    private string s = "";
}

The linked MSDN article will offer a fully description when there's no access modifier explicitly specified.

CraigTP
@Joel Coehoorn pointed out that he probably meant no access modifier.
IainMH
Ok. I've edited my answer to include information relating to that possibility.
CraigTP
+16  A: 

Access modifiers

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

Static

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

Static classes are often used as services, you can use them like so:

MyStaticClass.ServiceMethod(...);
GoodEnough
And you can have static methods in non-static classes, right?
SkippyFire
Yes, they would behave the same way as in my example.
GoodEnough
A: 

public - can be access by anyone anywhere. private - can only be accessed from with in the class it is a part of. protected - can only be accessed from with in the class or any object that inherits off of the class.

Nothing is like null but in VB. Static means you have one instance of that object, method for every instance of that class.

Tony
A: 

Hi,

I think it is related to good OOP design. If you are developer of library you want to hide inner working of your library, so that you can modify your library inner working later on. So you put your members as private and helper methods as private and only interface methods are public. Those methods that should be overwrited should be protected.

Darius Kucinskas
A: 

Careful watch your accessibility of your classes. Public and protected classes and methods are by default accessible for everyone.

Also Microsoft isn't very explict in showing access modifiers (public, protected, etc.. keywords) when new classes in Visual Studio are created. So, take good care and think about your accessibility of your class because it's the door to your implementation internals.

Patrick Peters
+1  A: 

Public - If you can see the class, then you can see the method

Private - If you are part of the class, then you can see the method, otherwise not.

Protected - Same as Private, plus all descendants can also see the method.

Static (class) - Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.

Static (method) - Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.

JosephStyons
Can't you have static methods in a non static class though?
SkippyFire
Yes, but I was talking about a static class. I added a separate entry to describe static methods. Thanks for the catch.
JosephStyons
+1  A: 

Regarding the question of Nothing

  • Namespace types are internal by default
  • Any type member, including nested types are private by default
leppie
A: 

from the above table we can see the deference between private and protected... am think both are same ....so what the need for that two separate command

ragesh cr
A: 

A status of Private indicates that variables can only be accessed by objects of the same class. Protected status extends that access to include descendants of the class as well.

"from the above table we can see the deference between private and protected... am think both are same ....so what the need for that two separate command"

Grant Hood