# Instance Classes

Instance classes are classes that are called as **objects**. These classes have certain things unique to them such as constructors.

***

### Creating class objects (instance classes)

Objects are similar to variables in that they hold values and data. Similar to the String data type, they are also reference types and can hold methods. You can create these classes similar to creating a variable as well. Here is the Java syntax:

```java
<className> <object name> = new <className>();
```

***

### Constructors (Only for instance classes)

Class constructors are methods that run once automatically when an object of that class object  is created.  Class constructors allow you to add parameters, allowing for more versatility with class objects.&#x20;

#### Creating class constructors

Declaring a constructor is similar to declaring a class method with parameters, the only difference is that you don't declare a return type and the constructor must have the same name as the class, this is because they never return anything. Constructors are usually used to assign variables to certain values. Here is an example of creating a constructor in a new `example` class that assigns `exampleInt` to a given parameter:

```java
public class Example{
    int exampleInt; // declaring a variable without a value
    public example(int exampleInt){
        this.exampleInt = exampleInt;
    }
    public void addToInt(int add){
        exampleInt += add;
    }
}
```

One thing in the code above that you may not know is the `this` identifier. The `this` identifier allows you to refer to variables of the same name outside of the scope of a method.&#x20;

Scope refers to the region of code where a piece of data (like a variable) can be used since variables or parameters instantiated inside a method cannot be directly accessed from outside the method's { } brackets:

<img src="/files/jYSi25fipRtMIraWm9PP" alt="" class="gitbook-drawing">

#### Using constructors

Using class constructor parameters is basically the same as using method parameters. This is because all you have to do is put your values inside the parentheses (). Here is an example of the `example` class object `ex`:

<pre class="language-java"><code class="lang-java"><strong>/* Create a new object and use the 
</strong><strong>constructor to assign a value to exampleInt */
</strong>Example ex = new Example(17); 
// Now exampleInt in the ex object is 17
</code></pre>


---

# 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://velocity-raptors-9450.gitbook.io/intro-to-frc-programming-romi/intro-to-java/object-oriented-programming/instance-classes.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.
