# Classes and Objects

## 🏗️ What is a Class?

A class is essentially a template or blueprint that defines what objects within a Java program will look like and be able to do. Think of it like a cookie cutter - the class is the mold that objects get "baked" from during runtime.

Within a class you define variables and methods. Variables store data, like a cookie's shape and color. Methods are actions the objects can perform, like rotating or growing.

<mark style="background-color:yellow;">**When you create a class, it doesn't actually make any objects yet.**</mark> But any objects created with "new" will be of that class type.

For example:

```java
public class Person {

  //Within the curly brackets is where you'll define your variables and methods.
  
}
```

The `Person` here is a class. It acts as a blueprint for creating `Person` objects.

## 🏛️ Rules for Classes and Objects in Java 🏛️

Classes and objects are fundamental concepts in Java's object-oriented programming paradigm. There are certain rules around defining and using classes and objects.

### 📜 Rules for Classes 📜

Some key rules for defining classes in Java are:

* A class must have a name that is a valid identifier
* The class body must be between opening and closing braces {}
* A class can contain fields, methods, constructors, nested classes etc.
* Fields and methods can have any access modifier (public, private, protected, default)
* Only one public class per source file is allowed

For example:

```java
public class Person {

  private String name;

  public Person(String name) {
    this.name = name;
  }

}
```

This defines a `Person` class with a private field and public constructor.

### 🎁 Rules for Objects 🎁

Some important rules related to objects are:

* Objects must be instantiated using the `new` keyword before use
* Data fields in an object can be accessed using the `.` operator
* Methods of an object can be invoked using the `.` operator
* The `this` keyword refers to the current object instance
* Objects in Java are passed by reference, not value

For example:

```java
Person person = new Person("John"); 

System.out.println(person.name); // Access field

person.walk(); // Call method
```

This creates a `Person` object and accesses its fields and methods.

So those are some of the key rules around defining and working with classes and objects in Java. Following these rules allows creating robust object-oriented code.

## 🤓 Key Notes

* A class is declared using the `class` keyword
* It describes what attributes and behaviors an object will have
* Objects are created (instantiated) based on the class
* The class is a template, objects are instances of the template

So in summary, a **class** provides a definition for the attributes and behaviors of objects that will be created from it. It's a blueprint describing what the object will contain.

## 💎 In Simple Terms

A class is like a mold for making objects - it defines what the objects made from that mold will be like.

Objects are created from a class mold that describes them. The class itself is just the template, not the actual object.

So in Java, you define classes which act as blueprints for objects!

<figure><img src="https://images.unsplash.com/photo-1606761568499-6d2451b23c66?crop=entropy&#x26;cs=srgb&#x26;fm=jpg&#x26;ixid=M3wxOTcwMjR8MHwxfHNlYXJjaHwzfHxjbGFzc3xlbnwwfHx8fDE2ODk5MTY2ODJ8MA&#x26;ixlib=rb-4.0.3&#x26;q=85" alt=""><figcaption></figcaption></figure>

## 🥽 What is an Object?

An **object** in Java is an instance of a class. It is created from a class blueprint and contains its own state and behaviors.

<figure><img src="https://static.javatpoint.com/images/characteristics-of-object.jpg" alt=""><figcaption></figcaption></figure>

For example:

```java
public class Person {
  // Class definition
}

Person obj = new Person(); // Object instantiation 
```

Here `obj` is an object of type Person that is instantiated from the `Person` class.

## 🍃 Key Notes

* An object is an instance of a class
* It is created via new keyword from a class
* It has its own set of attributes and behaviors
* Multiple objects can be made from a single class

So an object is basically a real world entity represented inside a program.

<details>

<summary>Class and Object Problems</summary>

1. What is a class? (A template that defines common properties and behaviors of an object)
2. What is an object? (An instance of a class)
3. How do you define a class in Java? (Use the class keyword followed by the name)
4. How do you create an object from a class? (Use the new keyword followed by the class name)
5. How do you add data fields/attributes to a class? (Define variables within the class)
6. How do you add methods to a class? (Define functions within the class using the public or private keyword)
7. How do you access object attributes? (Use dot notation objectname.attribute)
8. How do you access object methods? (Use dot notation objectname.method())
9. What is a constructor? (A special method that creates and initializes an object)
10. How do you pass values to a constructor? (Include parameters in the constructor definition and pass them when creating the object)

</details>

<figure><img src="https://images.unsplash.com/photo-1563219996-45f1a0ba692e?crop=entropy&#x26;cs=srgb&#x26;fm=jpg&#x26;ixid=M3wxOTcwMjR8MHwxfHNlYXJjaHw2fHxvYmplY3R8ZW58MHx8fHwxNjg5OTE2NzA0fDA&#x26;ixlib=rb-4.0.3&#x26;q=85" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/java/oops/classes-and-objects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
