# Class Constructor Block not working

**URL:** https://discourse.processing.org/t/class-constructor-block-not-working/16282
**Category:** Coding Questions
**Created:** [December 10, 2019, 5:00am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282 "2019-12-10T05:00:27Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 5:00am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/1 "2019-12-10T05:00:27Z")

</div>

So I’m just starting out on processing and I run into this error:  
Unexpected token: {  
Here is my code:

```auto
Person joe;

void setup(){
  size(500,500);
  background(0);
  Person joe = new Person();
}

void draw(){
  joe.display();
}

Class Person(){
  String shape;
  color c;
  Person(){
    c=color((mouseX+mouseY)/4);
    if (mouseX/2<=200){
      shape="rectangle";
    }else{
      shape="ellipse";
    }
  }
  
  void display(mouseX,mouseY){
    stroke(c);
    fill(c);
    if(shape=="rectangle"){
      rect(mouseX,mouseY,10,10);
    }else if(shape=="ellipse"){
      ellipse(mouseX,mouseY,10,10);
    }
  }
}

```

Can anyone explain this? I thought I followed the tutorial pretty well. Error occurs within the class, at

```auto
Person(){

```

---

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 5:01am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/2 "2019-12-10T05:01:23Z")

</div>

Sorry about the indenting, I’m new. How do I get it to show the correct indentation?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [December 10, 2019, 5:13am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/3 "2019-12-10T05:13:30Z")

</div>

You can use the \</\> sign at the top when posting/editing.

As for the error, that generally means you are missing a } or you have too many {. Though you should have gotten a line number with that error Message i think…

Nevermind, you forgot a ; in void display() after drawing the rect.

---

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 7:09am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/4 "2019-12-10T07:09:10Z")

</div>

Damn semicolons! Still didn’t fix the error though.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2019, 7:32am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/5 "2019-12-10T07:32:07Z")

</div>

```auto
Person joe;

void setup() {
  size(500, 500);
  joe = new Person();
}

void draw() {
  background(200, 200, 0);
  joe.display();
}

class Person {
  String shape;
  color c;
  int w = 30;

  Person() {
  }

  void display() {
    c = color((mouseX+mouseY)/4, 0, 200);
    if (mouseX <= width/2) shape="rectangle";
    else shape="ellipse";
    stroke(c);
    fill(c);
    if ( shape.equals("rectangle") ) rect(mouseX, mouseY, w, w);
    else if ( shape.equals("ellipse") ) ellipse(mouseX, mouseY, w, w);
  }
}

```

from this working version, you please read back / compare to your original  
and see some small changes.  
[https://processing.org/reference/class.html](https://processing.org/reference/class.html)  
[https://processing.org/reference/String\_equals\_.html](https://processing.org/reference/String_equals_.html)

you can not just write a class free style,  
better start from a working example and change line by line to your target function

one main point is to know that that part of a class

```auto
Person() {
 }

```

works like a setup at creation,  
there can ?predefine? may things…  
but not use dynamic data like mouseX…

 ![class_person](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/85f438e4476fb325c59ccd476dac6b43b10ea82e.png)

---

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 7:40am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/6 "2019-12-10T07:40:11Z")

</div>

@kll copy-pasted your code into my IDE, same result. Beginning to suspect a bug? Will try restarting IDE.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2019, 7:46am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/7 "2019-12-10T07:46:58Z")

</div>

> [@hamp](#):
>
> same result.

please post a picture of the full PDE window to see the error after start

---

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 7:48am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/8 "2019-12-10T07:48:59Z")

</div>

@kll

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/58bd7eb425fdd316ae394b8033daac68da4102d0.png)

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2019, 7:53am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/9 "2019-12-10T07:53:14Z")

</div>

you not copy my full code!!!  
please try again ( copy / paste ) into a new PDE window

---

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 8:14am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/10 "2019-12-10T08:14:10Z")

</div>

@kll Same result.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8fc9966d8cb44333c6efcee6cb6d0a6b63344cb0.png)

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2019, 8:15am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/11 "2019-12-10T08:15:47Z")

</div>

this is not my code, why you not open new window and paste my code  
instead try repair your code without knowing what lines are wrong ( 14 )

---

<div class="post-metadata">

### Author: ![hamp](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hamp](https://discourse.processing.org/u/hamp)
#### Post date: [December 10, 2019, 8:25am UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/12 "2019-12-10T08:25:37Z")

</div>

@kll Sorry, new to processing, that works now. Thanks! (Still struggling to find the effective difference)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 16, 2019, 11:55pm UTC](https://discourse.processing.org/t/class-constructor-block-not-working/16282/13 "2019-12-16T23:55:34Z")

</div>

> [@hamp](#):
>
> Still struggling to find the effective difference

Well, there are a _lot_ of changes here, and it can be difficult to understand when they aren’t presented one at a time.

As a general strategy, you can use any programming text editor that shows `diff` to look at the differences between the two. It adds a + in front of added lines and - in front of removed lines.

Diffs can still be hard to read if lots of whitespace changes have been made, so I manipulated things slightly to line them up so you could more easily see some of the things going on in the changes:

```diff
Person joe;
 
void setup() {
   size(500, 500);
- background(0);
- Person joe = new Person();
+ joe = new Person();
 }
 
void draw() {
+ background(200, 200, 0);
   joe.display();
 }

```

Okay, move your background command into draw, so that it clears the screen every frame for redrawing. Also, in setup **assign** `joe` as the global variable – don’t **declare** a new local variable `Person joe`, which only exists inside setup.

```diff
-Class Person(){
+class Person {
   String shape;
   color c;
+ int w = 30;

```

Next, declare a class with the `class` keyword – lowercase, `Class` isn’t a thing. Classes also don’t take method arguments, instead have constructors, so drop the (). Add `w` as a class object variable – this will mean that objects aren’t hard-coded to 10, their size can be changed.

```diff
- Person(){
- c=color((mouseX+mouseY)/4);
- if (mouseX/2<=200){
- shape="rectangle";
- }else{
- shape="ellipse";
- }
+ Person() {
   }

- void display(mouseX,mouseY){
+ void display() {
+ c = color((mouseX+mouseY)/4, 0, 200);
+ if (mouseX <= width/2) shape="rectangle";
+ else shape="ellipse";
     stroke(c);
     fill(c);

```

Move all that stuff from the constructor into the display method. mouseX and mouseY are already global variable in Processing, so you don’t _need_ to pass them in, you can just use them.

```diff
- if(shape=="rectangle"){
- rect(mouseX,mouseY,10,10);
+ if(shape.equals("rectangle")){
+ rect(mouseX,mouseY,w,w);
- }else if(shape=="ellipse"){
- ellipse(mouseX,mouseY,10,10);
+ } else if(shape.equals("ellipse")){
+ ellipse(mouseX,mouseY,w,w);
     }
   }
}

```

Express dimensions in terms of w, not hard-coded with 10. Also, check string equality `.equals()` – don’t use ==.
