# Private Fields, Getters & Static

> Protect your internal data with private fields, control access with getters, and attach utilities directly to the class itself.

Subject: JavaScript | Lesson 24 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/private-fields-and-static
Previous: [Classes: Syntax & Inheritance](https://codewithanimation.in/javascript/classes.md) | Next: [this in Classes & Method Binding](https://codewithanimation.in/javascript/this-in-classes.md)

## Private Fields (#)

By default, all properties inside a JavaScript class are public, meaning anyone can access and change them.

By prefixing a property name with a hash (`#`), you create a **private field**. Private fields can only be read or modified from inside the class's own methods. If outside code tries to touch them, JavaScript throws a syntax error.

## Getters and Setters

If you make a property private, you might still want outside code to read it, but not change it. Or, you might want to validate data before letting someone change it.

You can define `get` and `set` methods. To the outside world, these look exactly like normal properties (accessed without parentheses), but under the hood, they execute your custom logic.

## Static Methods and Properties

Normally, methods are attached to the instances you create with `new`. But sometimes you want a utility function that belongs to the class itself.

By adding the `static` keyword before a method or property, you attach it directly to the class blueprint. You call them on the class itself, not on the instance (e.g., `Math.random()` or `Array.isArray()`).

## Common mistake: The old underscore convention

Before `#private` fields existed, developers used to prefix properties with an underscore (like `this._password`) as a 'gentleman's agreement' that the property shouldn't be touched. However, this didn't actually prevent access, it was just a warning.

## Tip: Static 'this'

Inside a static method, the `this` keyword does NOT point to an instance of the object. It points to the class itself. You cannot access instance properties from inside a static method!

## Try it: Class Architecture

Analyze a User class that utilizes private fields for security, getters for safe access, and static properties for global tracking.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/private-fields-and-static

## Check yourself

### Question 1

What happens if you try to access a private field like `user.#password` from outside the class?

- A. JavaScript throws a Syntax Error.
- B. It returns undefined.
- C. It returns the value, but logs a warning.

Correct answer: A

Private fields are strictly enforced by the JavaScript engine. They physically cannot be accessed from outside the class body.

- Why not B: Incorrect. The browser's engine strictly blocks it with an error.
- Why not C: Incorrect. It is a hard error.

### Question 2

How do you invoke a getter method named `get fullName()`?

- A. user.fullName()
- B. user.fullName
- C. user.getFullName()

Correct answer: B

Getters are accessed exactly like regular properties, without parentheses. The engine executes the method automatically behind the scenes.

- Why not A: Incorrect. You do not use parentheses.
- Why not C: Incorrect. That is a normal method, not a getter.

### Question 3

Which of the following is true about `static` methods?

- A. They are called on the class itself, not on instances.
- B. They can easily access all of an instance's properties.
- C. They are automatically run when an instance is created.

Correct answer: A

Static methods belong to the blueprint itself (e.g., `User.count()`), so they do not know about individual instances created from that blueprint.

- Why not B: Incorrect. Static methods do not have access to instance properties.
- Why not C: Incorrect. That is the `constructor`.

## Remember this

- Prefix a field with `#` to make it strictly private to the class.
- `get` and `set` methods allow you to execute logic when a property is accessed or changed.
- Getters and setters are accessed like normal properties (no parentheses).
- `static` methods belong to the class itself, not the instances.
- Static methods are great for utility functions or tracking data across all instances.
