# How do I define an object with curly brackets?

**URL:** <https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872>\
**Category:** Coding Questions\
**Created:** [August 23, 2018, 3:35am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872 "2018-08-23T03:35:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:35am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/1 "2018-08-23T03:35:11Z")

</div>

I want to define objects in an array like this:

```auto
{name: "name", temp: 123, ...}

```

How would I do this?

-Grim

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 23, 2018, 3:37am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/2 "2018-08-23T03:37:44Z")

</div>

Are you talking about Java or JavaScript?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2018, 3:38am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/3 "2018-08-23T03:38:53Z")

</div>

You could create a `class` for those properties: 🤓

```auto
class Stuff {
  constructor(name, temp) {
    this.name = name, this.temp = temp;
  }
}

```

> **[constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor)**
>
> The constructor method is a special method for creating and initializing an object created within a class.

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:38am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/4 "2018-08-23T03:38:56Z")

</div>

I am talking about Java.

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:39am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/5 "2018-08-23T03:39:22Z")

</div>

Well, I did. I made a class.

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:40am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/6 "2018-08-23T03:40:37Z")

</div>

I doesn’t work for Java I don’t think.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2018, 3:41am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/7 "2018-08-23T03:41:33Z")

</div>

Well, the curly `{}` syntax is JS. Therefore I’ve replied to you assuming it was JS! 🙄

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:41am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/8 "2018-08-23T03:41:56Z")

</div>

Is there anything like this in Java?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2018, 3:44am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/9 "2018-08-23T03:44:22Z")

</div>

> [@Grimtin10](#):
>
> Is there anything like this in Java?

Yes, the JS `class` code below: 💻

```auto
class Stuff {
  constructor(name, temp) {
    this.name = name, this.temp = temp;
  }
}

```

Would be like this in Java Mode: ☕

```auto
class Stuff {
  String name;
  int temp;

  Stuff(String name, int temp) {
    this.name = name;
    this.temp = temp;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:45am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/10 "2018-08-23T03:45:06Z")

</div>

I meant the curly bracket definitions.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2018, 3:47am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/11 "2018-08-23T03:47:11Z")

</div>

Java creates objects exclusively via classes and the operator `new`. 🧐  
JS is more flexible. But still I like the `class` approach better, even in JS. 🤪

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [August 23, 2018, 3:47am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/12 "2018-08-23T03:47:55Z")

</div>

Alrighty then. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 23, 2018, 4:28am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/13 "2018-08-23T04:28:16Z")

</div>

Java doesn’t have the syntax you’re talking about.

But note that if you have a class with a constructor:

```auto
class MyClass{
  String name;
  int temp;
  public MyClass(String name, int temp){
    this.name = name;
    this.temp = temp;
  }
}

```

Then you can call that constructor and place the instances in an array, like this:

```auto
MyClass[] myArray = { new MyClass("name", 123), new MyClass("Grimtim10", 42) }

```

Shameless self-promotion: here is a guide on creating classes in Processing:

> **[Creating Classes](https://happycoding.io/tutorials/processing/creating-classes)**
>
> Learn how to create classes in Processing.

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 23, 2018, 5:33am UTC](https://discourse.processing.org/t/how-do-i-define-an-object-with-curly-brackets/2872/14 "2018-08-23T05:33:07Z")

</div>

Hi @Grimtin10!  
What you’re referring to is called a hashmap in Java, dictionary in Python, and kind of similar to JSON in JavaScript. In Java, you can use the [HashMap data structure](https://processing.org/reference/HashMap.html):

```auto
HashMap<String, Object> dict= new HashMap<String, Object>();

void setup() {
  size(400, 400);
  dict.put("name", "Grimtin10");
  dict.put("temp", 123);

  println(dict);
  println(dict.get("name"));
  println(dict.get("temp"));
}

```
