# Deeply-layered nested classes

**URL:** <https://discourse.processing.org/t/deeply-layered-nested-classes/805>\
**Category:** Coding Questions\
**Created:** [June 9, 2018, 9:29am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805 "2018-06-09T09:29:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [June 9, 2018, 9:29am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/1 "2018-06-09T09:29:10Z")

</div>

Hello.  
I’m trying to make nested classes to make some sort of system in my classes. I am working insode Visual Studio Code, where you can hide lines of code.

When I try:

```auto
static class A{
    static class B{
        class C{
            C(){}
        }
    }
}

A.B.C ins = new A.B.C();

```

it gives me an error: …\*\* No enclosing instance of type Test.A.B is accessible. Must qualify the allocation with an enclosing instance of type Test.A.B (e.g. x.new A() where x is an instance of Test.A.B).\*

When I try

```auto
class A{
    A(){}
    class B{
        B(){}
        class C{
            C(){}
        }
    }
}

```

The only way it works is when I do:

```auto
A.B.C inst = ((new A()).new B()).new C();

```

Why can’t I make nested classes like 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:** [June 9, 2018, 2:40pm UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/2 "2018-06-09T14:40:06Z")

</div>

- If your code is inside “.pde” files, all your classes & interfaces are nested to a PApplet subclass.
- As a workaround, you can declare them all as `static`, so they behave like top classes.
- Or move them to a “.java” file. Processing’s IDE (PDE) recognizes “.java” files in addition to “.pde”.

---

<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:** [June 9, 2018, 2:51pm UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/3 "2018-06-09T14:51:52Z")

</div>

In this example, all of the 3 nested classes are declared as `static`:

```auto
void setup() {
  A.B.C c = new A.B.C();
  exit();
}

static class A {
  static class B {
    static class C {
    }
  }
}

```

And in this 1, the innermost class C isn’t `static`. We need to instantiate class A.B before instantiating class C:

```auto
void setup() {
  A.B.C c = new A.B().new C();
  exit();
}

static class A {
  static class B {
    class C {
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [July 5, 2018, 6:30am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/4 "2018-07-05T06:30:39Z")

</div>

Thanks for the reply.  
So, the outer most class is the PAplet. Is it static or not?

---

<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:** [July 5, 2018, 6:34am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/5 "2018-07-05T06:34:11Z")

</div>

That’s right! It is a PApplet subclass. 🕶  
All top classes are implicitly `static` by nature. 🤓

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [July 5, 2018, 9:30am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/6 "2018-07-05T09:30:00Z")

</div>

Ok, so I tried this:

```auto
static class A{
  class B(){
    
  }
}

A.B b = A.new B();

```

And the console said:

```auto
Status ERROR: org.eclipse.jdt.core code=4 Exception occurred during compilation unit conversion:
----------------------------------- SOURCE BEGIN -------------------------------------
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class sketch_180705a extends PApplet {
public void setup() {
static class A{
  class B{
    
  }
}

A.B b = A.new B(

}
}

----------------------------------- SOURCE END ------------------------------------- java.lang.IllegalArgumentException

```

So, there is a new bug I guess.

This works inside the PApplet subclass:

```auto
class A{
  class B{
    
  }
}

A.B b = new A().new B();

```

```auto
class A{
  class B{ 
    class C{
      
    }
  }
}

A.B.C b = new A().new B().new C();

```

Also, I can’t set any class to be static.

---

<div class="post-metadata">

**Author:** ![mat650](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mat650/32/564_2.png) [@mat650](https://discourse.processing.org/u/mat650)\
**Post date:** [July 5, 2018, 9:55am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/7 "2018-07-05T09:55:43Z")

</div>

I have tried in IntelliJ IDEA and the code modify this way works for me:

```auto
package com.processing.sketch;

import processing.core.PApplet;

public class Sketch extends PApplet {

    public void settings() {
    }

    public void setup() {
        A.B.C ins = new A().new B(). new C();
    }

    public void draw() {
    }

    public static void main (String[] args) {

        Sketch object = new Sketch();
        PApplet.runSketch(new String[]{"Sketch"}, object);
    }

    class A{
        A(){}
        class B{
            B(){}
            class C{
                C(){}
            }
        }
    }

}

```

Let me know if it dose what you want to achieve and if it works in your environment as well.

---

<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:** [July 5, 2018, 1:39pm UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/8 "2018-07-05T13:39:19Z")

</div>

> [@FurryNightShade](#):
>
> Ok, so I tried this:
> 
> ```auto
> static class A{
> class B(){
>     
> }
> }
> 
> A.B b = A.new B();
> 
> ```

As I had stated already: 🙄

> [@GoToLoop](#):
>
> If your code is inside “.pde” files, all your classes & interfaces are nested to a PApplet subclass.

In other words: We can’t have top classes or interfaces inside “.pde” files! 😢  
Most we can do is declaring them as `static`. That’s as far as we can get to actual top classes’ behavior: 😑

```auto
static class A {
  static class B {
  }
}

A.B ab;

void setup() {
  ab = new A.B();
  exit();
}

```

And again, as I had previously advised you at that very same post: 🧐

> [@GoToLoop](#):
>
> Or move them to a “.java” file. Processing’s IDE (PDE) recognizes “.java” files in addition to “.pde”.

Simply place those classes & interfaces inside a “.java” file. They’d become actual top classes & interfaces. ☕

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [July 6, 2018, 12:11pm UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/9 "2018-07-06T12:11:54Z")

</div>

Yes, but I don’t want want to move outside the Aplet subclass, because I need to draw stuff.  
Also I can’t declare any class static if there is another class in it. And in fact I can’t even declare the inner class static. The error displaying field says, I can only declare them virtual on abstract.

I have an idea, that I could outside of PAplet subclass create PGraphics to draw in and then inside the PApplet subclass just draw all PGraphics instances.

Or, I could get the instance of the PGraphics inside the PApplet subclass and draw directly in it outside of the PAplet subclass. I don’t know how to get the PGrapgics instance tho.

I need to work inside the PApplet class only because I need to be able to draw. If I could draw outside of it, I’d use .java files.

mat650: That is basically what I tried…

---

<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:** [July 6, 2018, 5:43pm UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/10 "2018-07-06T17:43:34Z")

</div>

> [@FurryNightShade](#):
>
> Yes, but I don’t want want to move outside the PApplet subclass, because I need to draw stuff.

Placing code in “.java” files instead of “.pde” was just an advise.  
Declaring nested classes as `static` is pretty close to the same behavior of top classes in “.java” files after all.

However, whether a class is top, static or inner, got nothing to do w/ drawing stuff or not!

If a top class couldn’t do it like you state, how is it possible that libraries written for Processing are able to?

The trick is to request the PApplet’s reference in their constructor, store it as a field, and then use it in order to draw on that PApplet’s canvas.

Nested static classes can do the same as top classes. Just demand the PApplet’s reference.

---

<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:** [July 6, 2018, 5:47pm UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/11 "2018-07-06T17:47:40Z")

</div>

> [@FurryNightShade](#):
>
> I don’t know how to get the PGraphics instance though.

Once a class has a hold of some running PApplet reference, it can grab its canvas by invoking **getGraphics()**.

Although we don’t really need to; given we can invoke drawing methods directly through the PApplet reference.

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [July 9, 2018, 5:18am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/12 "2018-07-09T05:18:02Z")

</div>

Oh, then that is why all kinds of classes from libraries, that can draw, need to be initalized outside of any class inside the outer PAplet subclass in order to get the **this** to return PApplet, so that it can get a reference to the PAplet subclass.

I got more from this topic, than I asked for. Thanks very much. 🙂

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [July 9, 2018, 5:19am UTC](https://discourse.processing.org/t/deeply-layered-nested-classes/805/13 "2018-07-09T05:19:04Z")

</div>

By the way, how do you mark a topic answered?
