C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
This means other classes, other files. Used on members, classes and method declarations, it is not the default accessibility. We often specify public with static.
Example. We show how to define public methods. We then call those methods from outside the class. Public is an accessibility modifier. It is not the default, which is private. We specify public to make methods accessible from other classes.
Static: Public methods can be static. This means they are attached to the type itself.
Also: They can be instance—this means they are attached to an instance of the type that was constructed.
C# program that uses public methods using System; public class Example { static int _fieldStatic = 1; // Private static field int _fieldInstance = 2; // Private instance field public static void DoStatic() { // Public static method body. Console.WriteLine("DoAll called"); } public static int SelectStatic() { // Public static method body with return value. return _fieldStatic * 2; } public void DoInstance() { // Public instance method body. Console.WriteLine("SelectAll called"); } public int SelectInstance() { // Public instance method body with return value. return _fieldInstance * 2; } } class Program { static void Main() { // First run the public static methods on the type. Example.DoStatic(); Console.WriteLine(Example.SelectStatic()); // Instantiate the type as an instance. // ... Then invoke the public instance methods. Example example = new Example(); example.DoInstance(); Console.WriteLine(example.SelectInstance()); } } Output DoAll called 2 SelectAll called 4
This program contains two classes: the Example class, which contains the public methods we are examining, and the Program class, which contains the Main entry point. There are four public methods in the Example class.
Details: Two are static methods, two are not static methods, and half of them are void methods with no return values.
In the Main entry point, we can call DoStatic and SelectStatic through the type itself. The Example class can also be created with the new operator. DoInstance and SelectInstance can be invoked this way.
Types versus instances versus objects. It is important to know the difference between types and instances in programming languages. A type can be thought of as a template from which instances and objects can be created.
Tip: The type stores no state. It can specify the layout of objects that can store state.
Static fields and static data are not part of the type, and can be thought of as an instance that cannot be created more than once. The .NET Framework's execution engine, even its low-level instructions, is object-oriented.
Default. Methods are implicitly private and instance. You must specify any deviations from this default state. Any method that needs to be public must declare the public modifier. And static (type-based) methods must declare the static modifier.
Note: This default encourages the practice of information hiding in programming. And this improves software quality.
Summary. This example explored the public modifier as it applies to instance and static methods. We noted how methods are not by default public. This modifier must be explicitly specified. Too many public methods indicate a poor design.
Instead: Separate classes with more private methods is better. It promotes information hiding.