# Help with static methods!

**URL:** https://discourse.processing.org/t/help-with-static-methods/2228
**Category:** Coding Questions
**Created:** [July 31, 2018, 5:48am UTC](https://discourse.processing.org/t/help-with-static-methods/2228 "2018-07-31T05:48:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![michaelberge](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@michaelberge](https://discourse.processing.org/u/michaelberge)
#### Post date: [July 31, 2018, 5:48am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/1 "2018-07-31T05:48:20Z")

</div>

I’m getting an error that says ‘No enclosing instance of type \_\_\_\_ is accessible’. I basically want a class that has both non-static and static variables but Processing doesn’t allow that for some reason. I tried putting static methods in the Matrix class but I still got an error.

```auto
static class Matrix_Static {
   // Multiply Matrix m1 by Matrix m2
  static Matrix multiply(Matrix m1, Matrix m2) {
    // Matrix Product (a • b)
    if(m1.cols == m2.rows) {
       Matrix result = new Matrix(m1.rows, m2.cols);
      for(int i = 0; i < result.rows; i++) {
        for(int j = 0; j < result.cols;j++) {
          float sum = 0;
          for(int k = 0; k < m1.cols; k++) {
            sum += m1.data[i][k] * m2.data[k][j];
          }
          result.data[i][j] = sum;
        }
      }
      return result;
    } else {
      println("error - conflicting dimensions");
      return null;
    }
  }
}

class Matrix extends Matrix_Static {
  int rows, cols;
  float[][] data;
  
  // Constructor 1
  Matrix(int rows, int cols) {
    this.rows = rows;
    this.cols = cols;
    this.data = new float[rows][cols];
    
    for(int i = 0; i < rows; i++) {
      for(int j = 0; j < cols;j++) {
        this.data[i][j] = 0;
      }
    }
  }

```

---

<div class="post-metadata">

### Author: ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)
#### Post date: [July 31, 2018, 5:54am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/2 "2018-07-31T05:54:37Z")

</div>

Assuming this is Java, it sounds like you’re more concerned with how static methods and variables in Java.

Can I ask what you already know about building classes and using static methods/variables?

`Also, to add code, surround the code with triple tildes`

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 31, 2018, 5:56am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/3 "2018-07-31T05:56:55Z")

</div>

Hi!  
Can you please format your code using the `</>` button please?

Also, you can go have a look here for more general informations about posting:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<div class="post-metadata">

### Author: ![michaelberge](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@michaelberge](https://discourse.processing.org/u/michaelberge)
#### Post date: [July 31, 2018, 6:00am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/4 "2018-07-31T06:00:17Z")

</div>

Thanks for replying so quickly! So I have this Matrix class with a bunch of non-static methods that can only be called with an instance of a matrix class, but I want to add a static classes within the Matrix class so I can add, say, a method which is called multiply which takes 2 matrices and multiplies them together. This static method would be called by: Matrix.multiply(m1, m2); which would return a new Matrix. Processing does not allow me to do this for some reason…

---

<div class="post-metadata">

### Author: ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)
#### Post date: [July 31, 2018, 6:12am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/5 "2018-07-31T06:12:32Z")

</div>

Have you got it to work without implementing the class as a static class? I’ll be honest, I’m not an expert, but from my understanding, using static methods is generally a performance thing.

My advice? Try to get it working without static elements, and then implement static elements.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [July 31, 2018, 6:41am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/6 "2018-07-31T06:41:56Z")

</div>

In Java the keyword `static` does not have the same meaning as in c# for example and you can’t create an outer `static` class. The keyword should be used only for inner classes. See that link for further explanation:

> **[Java Static Class: A Tutorial on Static Classes in Java](https://www.caveofprogramming.com/java/java-static-class-tutorial.html)**
>
> Java, C++ and Perl Tutorials - Learn to program

PS: Again, can you please format your code in your first post? It is not because I was not giving you solution in my previous post that you need to ignore what is writtent in it…

---

<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 31, 2018, 6:50am UTC](https://discourse.processing.org/t/help-with-static-methods/2228/7 "2018-07-31T06:50:20Z")

</div>

> **[Deeply-layered nested classes](https://Discourse.Processing.org/t/deeply-layered-nested-classes/805)**
>
> 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: static class A{ static class B{ class C{ C(){} ...
