Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Back To Module Home

Object-oriented Programming
in C#

12% completed

Search Module

Introduction to Object-
Oriented Programming
in C#

Classes and Objects

Introduction to Objects and Classes


Got any feedback? Get in touch with us.
Declaration and Implementation

Accessing the Class Members

Access Modifiers

Fields

Methods

Static and Non-Static Methods

The 'this' Reference Variable


Static and Non-Static Methods
In this lesson, we will discuss static and non-static methods.

We'll cover the following

• Static and Non-Static Methods


• Static Methods
• Non-Static Methods

Static and Non-Static Methods


Methods can be of static or non-static, also known as instance methods, type. Let’s
have a brief discussion about these.

Static Methods
Got any feedback? Get in touch with us.
Static methods, just like static variables, can be called from another class without
instantiating/creating an object of the class. These methods can be called using the
class’ name. Let’s check this out in our code:

1 class VendingMachine {
2
3 static string manufacturer = "Vendy Inc.";
4
5 public static void PrintManufacturer() { //static method
6 Console.WriteLine("The manufacturer of machine is {0}", manufacturer);
7 }
8
9 }
10
11 class Demo {
12
13 public static void Main(string[] args) { //Note that the Main() is also a static method
14 //calling the static method using the class name
15 VendingMachine.PrintManufacturer();
16
17 }
18
19 }

Run Save Reset

Static methods cannot access non-static variables directly, i.e., without


creating an instance of a class.

Normally static methods are declared when we don’t need multiple instances of a
Got any feedback? Get in touch with us.
particular feature. For example, there can be only one Main() method in a C#
program because it serves as an entry point to that program. When the compiler
executes a program it looks for a static method named Main() because it knows it
has to run this method directly without the need to create an instance of a class. If
needed, we can actually have multiple Main() methods in a program but then we’ll
have to tell the compiler which Main() method should be used as the entry point.
Non-Static Methods
Non-static methods of a class can only be called with reference to a specific instance
of that class. Since the non-static methods are referenced using a specific object, we
can say that they can be used to perform an object-specific operation. Non-static
methods are also referred to as instance methods.

We have been working with instance methods until now. Let’s take a deeper look
into this with the help of a code.

1 class VendingMachine {
2
3 static string manufacturer = "Vendy Inc.";
4 private int _capacity = 100;
5
6 public void PrintSpecification() { //instance method
7 Console.WriteLine("The {0}'s machine has {1} products capacity", manufacturer, _capa
8 }
9
10 }
11
12 class Demo {
13
14 public static void Main(string[] args) {
15 VendingMachine vendingMachine = new VendingMachine();
16 Got any feedback? Get in touch with us.
//calling the instance method using the class object
17 vendingMachine.PrintSpecification();
18
19 }
20
21 }
Run Save Reset

The non-static methods can access both static and non-static fields.

In the next lesson, we will learn about the this reference variable.

Back Mark As Completed Next

Methods The 'this' Reference Variable

Got any feedback? Get in touch with us.

You might also like