# Can't acces class variables in function

**URL:** <https://discourse.processing.org/t/cant-acces-class-variables-in-function/5403>\
**Category:** Coding Questions\
**Created:** [November 10, 2018, 12:17pm UTC](https://discourse.processing.org/t/cant-acces-class-variables-in-function/5403 "2018-11-10T12:17:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benja8151](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/benja8151/32/2295_2.png) [@benja8151](https://discourse.processing.org/u/benja8151)\
**Post date:** [November 10, 2018, 12:17pm UTC](https://discourse.processing.org/t/cant-acces-class-variables-in-function/5403/1 "2018-11-10T12:17:13Z")

</div>

Hello,  
before I start, I must say I’m pretty much a total newbie when it comes to programming in Processing, or in Java in general. As you can see, I have two shape classes, rectangle and circle. I’m trying to implement a function `checkForNoOverlap` , which takes and array of already drawn shapes, and a new shape, which we have to check against every other one, if it’s overlapping. I’ve tried to use two `instanceof` expressions, to cover cases when we would compare two circles, two rectangles or a rectangle and a circle. However, right at the beginning, I have a problem - when I try to access selected shape’s coordinates with `object.rectX` , it says `"The global variable "rectX" does not exist."` Why doesn’t it allow me to access it?

Thank you for you help,  
Benjamin

Code:

> **Summary**
>
> ```auto
> class shape{
> }
> 
> class rectangle extends shape{
> int stroke = 9;
> int rectX;
> int rectY;
> int rectWidth;
> int rectHeight;  
> }
> class circle extends shape{
> int stroke=9;
> int circleX;
> int circleY;
> int diameter;
> }
> 
> ArrayList<shape> drawnShapes = new ArrayList<shape>();
> 
> boolean checkForNoOverlap(ArrayList<shape> drawnShapes, shape object){
> if (object instanceof rectangle){
> for (shape other: drawnShapes) {
> if (other instanceof rectangle){
> {
> if (Abs(object.rectX - other.rectX) > (object.rectWidth/2 + other.rectWidth/2)) {return true;} 
> if (Abs(object.rectY - other.rectY) > (object.rectHeight/2 + other.rectHeight/2)) {return true;}  
> return false;
> }
> }
> //if other object is circle...
> }
> }
> else{
> //if new object is circle....
> }
> }
> 
> ```

---

<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:** [November 10, 2018, 1:25pm UTC](https://discourse.processing.org/t/cant-acces-class-variables-in-function/5403/2 "2018-11-10T13:25:55Z")

</div>

Try initialising the rectX variable… or the shape objects in General.

---

<div class="post-metadata">

**Author:** ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)\
**Post date:** [November 10, 2018, 2:21pm UTC](https://discourse.processing.org/t/cant-acces-class-variables-in-function/5403/3 "2018-11-10T14:21:49Z")

</div>

You need to cast `object` to the right class like this:

```auto
if (object instanceof rectangle) {
  rectangle rect = (rectangle) object;
  println(rect.rectX, rect.rectY); // use it through rect
}

```

I would advise to make first letter of you class names uppercase, like `Rectangle` or `Shape`, and keep variables lowercase. It keeps it clear what is a class and what is a variable and it is a convention which nearly all Java programmers adhere to.
