# Class Method and Private Method

**URL:** <https://discourse.processing.org/t/class-method-and-private-method/2798>\
**Category:** Coding Questions\
**Created:** [August 20, 2018, 6:56pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798 "2018-08-20T18:56:04Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 20, 2018, 6:56pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/1 "2018-08-20T18:56:04Z")

</div>

Hi everybody, Im trying to use some class method that I create but it is not work cause of the error: The method “method\_name” cannot be declared static; static methods can only be declared in a static or top level type.  
My intention is use the method as used in Java. Full example so everybody understand

Main.pde

```auto
void setup()
{}
void draw()
{
  int a = Example.sum(5, 4); //This is the way that I want to use

  Example foo = new Example();
  //Useless method just created to let you understand difference between this and the above one!
  foo.do(5);
}

```

Example.pde

```auto
class Example()
{
//some variables
//some contructor

  void do(int param)
  {
    this.variables += param;//sum variables with parameters passed
  }

  public static int sum(int a, int b)
  {
    return a*b;
  }
}

```

Hopefully I had been clear!  
So this is the way I used to create a class method in Java so I can call it whenever I want pass my params and get the result but using processing Im not able, there is a way?  
Thank you

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 20, 2018, 7:16pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/2 "2018-08-20T19:16:50Z")

</div>

In Processing the sketch code is wrapped up as a top level class so any class declared inside it will be an _inner class_ the easy solution here is to declare the class static like this

```auto
static class Example
{

```

note there is no ‘()’ in a class declaration!

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 20, 2018, 8:15pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/3 "2018-08-20T20:15:06Z")

</div>

Yeah sorry forgot it there!!!  
Anyway I tried do that stuff but I get this error:  
Cannot make a static reference to the non-static method random(float, float) from the type PApplet  
I see that I have 3 random() functions and when I comment that the error goes to the next one!  
If I comment out all random function the class works! But I need that random function 😃

Well, Im doing a Matrix class, maybe you already have one to share with static method 😃

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 20, 2018, 8:20pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/4 "2018-08-20T20:20:14Z")

</div>

I test it out and it works but currently im unable to use random().  
Currently Im trying to work it out. If you know the answer really appreciate it!

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 20, 2018, 10:15pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/5 "2018-08-20T22:15:50Z")

</div>

This is simply not possible. You can’t call an instance method from a static context - what instance would you be calling the method on?! Unless you pass in the PApplet as an argument. _Why_ do you want this to be static anyway?

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 20, 2018, 10:37pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/6 "2018-08-20T22:37:17Z")

</div>

Cause I dont know any other method to do that, the previous code was broken due to this and I had to adapt adding more lines!  
Anyway, if it isn’t still possible I will wait for an update!

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 21, 2018, 1:12am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/7 "2018-08-21T01:12:49Z")

</div>

### _Please format your code_ 😊

It consist on these two steps:

1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: `ctrl+t`
2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: `</>`

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and \>\> **save** \<\< the edits.

## Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

---

<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 21, 2018, 4:14am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/8 "2018-08-21T04:14:42Z")

</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(){} } } } 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).\* Whe…

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 21, 2018, 8:52am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/9 "2018-08-21T08:52:26Z")

</div>

> [@Manatee](#):
>
> Anyway, if it isn’t still possible I will wait for an update!

Wait for what?! It’s your code that needs to change.

---

<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 21, 2018, 8:56am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/10 "2018-08-21T08:56:10Z")

</div>

> [@Manatee](#):
>
> … but currently I’m unable to use **random()**.

[Docs.Oracle.com/javase/10/docs/api/java/lang/Math.html#random()](http://Docs.Oracle.com/javase/10/docs/api/java/lang/Math.html#random())

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 21, 2018, 9:46am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/11 "2018-08-21T09:46:22Z")

</div>

But my man, using java im able to do what I described above using processing no!

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 21, 2018, 10:08am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/12 "2018-08-21T10:08:00Z")

</div>

It has been explained why this is the case. Waiting for something to change _that is not going to change_ is pointless! Try reading through again and understanding why this is happening, and why _the bug is in your code_!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 21, 2018, 10:32am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/13 "2018-08-21T10:32:51Z")

</div>

> [@Manatee](#):
>
> I test it out and it works but currently im unable to use random().

The following sketch code does what you want. It is obvious that you don’t understand the code so I suggest that you do some basic research on object orientated programming (OO) and start with _classes_ and _objects_

```auto
static class Example
{
  static PApplet papp;

  public static void init(PApplet papplet) {
    papp = papplet;
  }

  public static int sum(int a, int b) {
    return a+b;
  }

  public static float rand(float max) {
    return papp.random(max);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 21, 2018, 11:19am UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/14 "2018-08-21T11:19:23Z")

</div>

Yeah ok, I will check!

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 21, 2018, 12:27pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/15 "2018-08-21T12:27:58Z")

</div>

I introduce your adjustments and now the method random can only have 1 params!  
EDIT:  
All fixed, I see that I can import whatever I want from java libraries so I imported utils.Random and initialize a new Random() var!  
And now I finally use instance method 😃 Thank you quark for let me deep more!  
Anyway I have a little question, what difference between Math and Random?  
Becaue I can call Math.sqrt() without import nothing and when I call Random.nextInt() I cant!  
Is this problem due to static non static class?  
Reading between docs I saw that MAth is Public Final class, instead Random is just public class. IS this the main difference?

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 21, 2018, 12:41pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/16 "2018-08-21T12:41:23Z")

</div>

Big difference between Math and Random is static and non-static methods. `Random::nextInt` is a method on _an instance_ of the `Random` class, and has to be called on an instance. `Math::sqrt` is a static method, basically a method on the class rather than an instance, so does not have to be called _via_ an instance of `Math` (which you can’t create anyway).

Similarly, the normal Processing `random()` method is a method on _an instance_ of `PApplet`. You can call it in a sketch without calling it on an instance of `PApplet` only because your code is an instance (of a subclass) of `PApplet`. As soon as you try and call such a method from another static context, as here, you need to have the instance of `PApplet` to call it on. Note that @quark code is nothing like what you’re describing - they are passing in the instance of `PApplet` in the `init()` method.

Despite what you say earlier, what you’re describing isn’t possible in Java either - what you have working is something quite different!

> [@Manatee](#):
>
> Becaue I can call Math.sqrt() without import nothing and when I call Random.nextInt() I cant!  
> Is this problem due to static non static class?

No, `Math` is part of the `java.lang` package, all of which is imported by default. Processing also adds some extra default imports.

You can also import static methods - eg. `import static java.lang.Math.*;` and just call `sqrt()`. This will likely conflict with a lot of Processing methods if you do it in a sketch though.

---

<div class="post-metadata">

**Author:** ![Manatee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Manatee](https://discourse.processing.org/u/Manatee)\
**Post date:** [August 21, 2018, 9:12pm UTC](https://discourse.processing.org/t/class-method-and-private-method/2798/17 "2018-08-21T21:12:57Z")

</div>

OK thank you, im starting to understand better now!  
GG man 😃
