'Nature of Code' Chapter 2 Query

Hi,

Thanks for taking the trouble to click on this Topic.

I’m currently working my way thru Daniel Shiffman’s Learning Processing & the Nature of Code books, and have the following query about a section in the latter, chapter 2 (Forces), section 2.3, p.69.

He uses the following code for simulating the effects of wind on a bouncing ball (the wind is only in-effect when the mouse button is pressed):

if (mousePressed) {
PVector wind = new PVector(0.5,0);
mover.applyForce(wind);
}

My interpretation of his code (ignoring the line mover.applyFoce(wind;) is as follows: when the mouse is pressed, a PVector object named wind is created. If / when the mouse is pressed again, Processing then creates another PVector object, also named wind?

I would expect the code above to result in an error, because an object already called wind exists. However, there is no error, and the code runs fine. Could someone explain why this is the case, as I’m a bit confused. Does the instances of wind only last the length of the if statement? Clearly I’ve misinterpreted.

Any help etc. will be much appreciated.

Kind regards,

Scolty

2 Likes

Almost. When you create a variable inside a function, it is a local variable that only exists within that function, and only until that function is done. Trying to access that variable from a different function would be an error.

Variables that are created outside of functions are global variables, and can be used inside any function.

You can learn more about this by searching “variable scope”. I’m sure you’ll find numerous guides online that will explain it better than I can.

Thanks you for replying TfGuy44, it is really appreciated. Embarrassingly, I have been jumping between chapters in Learning Processing and missed the section on Local vs Global variables. I guess where I’m still getting confused is that the wind in the code above is an object, variables I think of as type boolean, int, float etc. But I tried calling wind elsewhere in the class, and as you advised, it didn’t work.

Thanks again for your help; I hadn’t anticipated such a prompt & constructive reply :- )

Hello, @scolty2021, and welcome to the Processing Forum!

A variable can refer to any type of object, so long as it is declared and then assigned correctly. In your code, wind refers to a PVector object that is being used to represent a force applied by wind.

This statement declares wind as a local variable that can reference a PVector object, then instantiates a PVector object, and finally assigns it to wind:

PVector wind = new PVector(0.5,0);

The following statement then calls an applyForce method from an object represented by a variable, mover, passing wind as an argument. Perhaps mover represents the ball, and this statement applies the force of the wind to the ball:

mover.applyForce(wind);

EDIT (March 8, 2021):

It only exists for the remainder of the block in which it was declared.

Following is an example from w3schools.com:Java Scope: Block Scope:

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x

  }
}

As given, it would run without an error.

However, if you modify it as follows, it would raise an error:

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x
  System.out.println(x); // This raises an error!
  
  }

Output:

Main.java:18: error: cannot find symbol
  System.out.println(x); // This raises an error!
                     ^
  symbol:   variable x
  location: class Main
1 error
1 Like

It’s not another PVector object, the old one gets overwritten

I think it ceased to exist.

correct

2 Likes

Hi javagar, I appreciate you replying to my post & for the welcome. Thank you also for clarify my mix-up regarding variables & objects - I’m very much a beginner programmer, so please forgive the very basic question.

Regarding your update, the wind PVector only lasts until the next closing bracket ‘}’, and because the ‘x’ in ‘System.out.println(x)’ in your last example is outside the block in which x is declared, an error occurs, that makes more sense.

Thanks again for your patience and taking the trouble to reply, it is much appreciated.

Kind regards,

Scolty.

1 Like

Hi Chrisir, thanks for also replying to my post - I wish I had joined the Processing forum sooner, having now realised how helpful and knowledgeable it’s members, like yourself, are.

Kind regards,

Scolty

3 Likes