New

Class

A class is a blueprint for creating objects. It defines what data an object holds and what it can do — all in one place.

Classes are the main building block of object-oriented programming. Instead of writing all your code in one file, you split it into classes — each one responsible for one thing.

Defining a Class#

A class has a name and a body. The body contains the data and behaviour that belong to it. The exact syntax depends on the language — see the language-specific pages below.

Class (C#)

A class in C# is a blueprint for creating objects, defined with the class keyword.

This page covers Class in C#. For a language-agnostic introduction, see Class.

Defining a Class#

public class Player
{
}

public is the access modifier — it means the class can be used from anywhere in the program. The class body sits between the curly braces.

By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding Fields and Properties#

A field stores data on the object. A property provides controlled access to that data.

Class (JavaScript)

A class in JavaScript is a blueprint for creating objects, defined with the class keyword.

This page covers Class in JavaScript. For a language-agnostic introduction, see Class.

Defining a Class#

class Player {
}

By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding a Constructor#

The constructor method runs automatically when an object is created. Use it to set the initial state.

class Player {
    constructor(name) {
        this.name = name;
    }
}

this.name = name stores the value on the object. this refers to the current instance.

Constructor

A constructor is a special method that runs automatically when an object is created from a class. Its job is to set up the object’s initial state.

  new Player("Alice")
  ┌─────────────────────┐
  │     constructor     │  ← runs automatically
  │  sets name = Alice  │
  └─────────────────────┘
  Player object ready

What a Constructor Does#

When you create an object, you often need to give it some starting values — a name, a score, a position. The constructor receives those values as arguments and stores them on the object.

Constructor (C#)

A constructor in C# is a special method inside a class that runs automatically when you create an object with new.

This page covers Constructor in C#. For a language-agnostic introduction, see Constructor.

Defining a Constructor#

A constructor has the same name as the class and no return type — not even void.

public class Player
{
    public string Name;

    public Player(string name)
    {
        Name = name;
    }
}

Name = name stores the argument on the object so it’s available later.

Constructor (JavaScript)

In JavaScript, the constructor is a special method inside a class that runs automatically when you create an object with new.

This page covers Constructor in JavaScript. For a language-agnostic introduction, see Constructor.

Defining a Constructor#

The constructor method is always named constructor. It receives arguments and uses this to store them on the object.

class Player {
    constructor(name) {
        this.name = name;
    }
}

Creating an Object#

Pass arguments to new when creating the object. JavaScript calls constructor automatically.

Object

An object is a specific instance created from a class. The class is the blueprint; the object is the thing built from it.

You can create as many objects as you need from the same class. Each one is independent — it has its own copy of the class’s data, separate from every other instance.

  ┌─────────────────┐
  │  class Player   │  ← blueprint (defined once)
  └────────┬────────┘
           │ instantiate
     ┌─────┴──────┐
     ▼            ▼
 ┌────────┐   ┌────────┐
 │ Alice  │   │  Bob   │  ← objects (created at runtime)
 │ score:3│   │ score:1│    each has its own data
 └────────┘   └────────┘

Class vs Object#

This distinction confuses almost every beginner. Here’s the clearest way to think about it: