class variables and the constructor

edited March 2014 in Using Processing

in my effort to understand the constructor i setup an experiment where i have taken the same piece of code and have altered constructor slightly

in the first example i have just given the class variables a value

in the second type i have passed arguments into the constructor and given the class variable the same value as those arguments.

both classes seem to do exactly the same thing(they have the same values) when tested as they just initialize the class variables. so i ask why has the constructor been built to pass arguments as values to the class variables and what are the advantages doing it this way is it just a stylistic choice?

perhaps it might be better to study java to delve into these questions more ?

class Mover {
      PVector location;
      PVector velocity;
      PVector acceleration;
      float mass;

      Mover() {
        mass =random(0.1,4);
        location = new PVector(0,0);
        velocity = new PVector(0,0);
        acceleration = new PVector(0,0);
}



class Mover {

  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;

  Mover(float m, float x , float y) {  // same values as the example above 
    mass = m;
    location = new PVector(x,y);
    velocity = new PVector(0,0);
    acceleration = new PVector(0,0);
}

Answers

  • btw the first example also starts with class Mover {

    i forgot to type it.

  • (fixed)

    say you want 5 Movers, starting in different positions. how would you do that?

  • Hello,

    imho both ways work

    the difference is as you stated that in the latter version you pass the values to the constructor, so you save 3 lines in setup() (where you would define the properties of the new object).

    Also I think it is considered good style to capsule the properties of the class so we don't touch them directly from the outside but use methods such as display() or move() e.g.

    it's more a philospohical question, isn't it?

    besides one class can have different constructors, see PVector where there is one for 2D and one for 3D

    Greetings, Chrisir

  • btw the first example also starts with class Mover { i forgot to type it.

    You can edit your posts! /:)

    Well, when we declare variables, we can choose to give them an initial value at the same time.
    And we can also decide not to have an explicit constructor. Java will provide a default 1 otherwise! :P

  • here

    Mover m1 = new Mover (4, 11, 22); // short and quick definition
    
    void setup() {
      background(111);
    }
    
    void draw() {
      background(111);
      m1.display() ;
    }
    
    class Mover {
    
      PVector location;
      PVector velocity;
      PVector acceleration;
      float mass;
    
      Mover(float m, float x, float y) {  
        // values 
        mass = m;
        location = new PVector(x, y);
        velocity = new PVector(1, 0);
        acceleration = new PVector(0, 0);
      }
      void display() {  
        ellipse(location.x, location.y, 15, 5);
        location.add(velocity);
      }
    }
    
  • edited March 2014 Answer ✓

    It is a good Object Oriented Programming practice to have classes and constructors according to logic. A class is a sort of a blueprint, upon which you create class objects.

    Your first Mover class uses "default" fixed values inside the constructor, which means when you create an object (Mover myObject = new Mover()) the object will get those values which can't be changed, unless you change them directly inside the class. Now what if you need the same functionality from the first class but with different parameters? Will you create another class? You can't change the position much nor the velocity nor the vector. There is a rule in OOP called do not repeat yourself and that's where the second constructor comes in.

    In the second constructor you can change the location. Now you can create multiple objects with different parameter values (different locations). The second constructor offers greater flexibility and you don' repeat the code. Not repeating gives you shorter cleaner code. Now you can for example spawn 5 boxes to different location on screen with the same class.

    Here is a simple example with multiple classes.

    FixedShape rectFixed;
    ShapeInConstructor rect;
    ShapeWithMethod rect1, ellipse;
    
    void setup() {
      size(500, 500);
      background(0);
      // Fixed constructor
      rectFixed = new FixedShape();
      // you can create a second object 
      // but you will not see it color red
      rectFixed = new FixedShape();
    
      // Multiple same shapes but in constructor
      // green color
      rect = new ShapeInConstructor(300, 200, 50);
      rect = new ShapeInConstructor(100, 100, 20);
      rect = new ShapeInConstructor(400, 234, 40);
    
      // Multiple different shape that use the same number of parameters for the 
      // settings but different shapes 
    
      rect1 = new ShapeWithMethod(80, 210, 15);
      ellipse = new ShapeWithMethod(30, 30, 45);
      rect1.displayRect();
      ellipse.displayCircle();
    }
    
    void draw() {
    }
    
    // The constructor has default values - color red
    class FixedShape {
      float xPos = 130;
      float yPos = 200;
      int size = 60;
    
      FixedShape() {
        fill(255, 0, 0);
        rect(xPos, yPos, size, size);
        fill(255);
      }
    }
    
    // The constructor has different values as parameters but same shape is the same
    // color green
    class ShapeInConstructor {
      int size;
      float xPos, yPos;
    
      ShapeInConstructor(float x, float y, int s) {
        xPos = x;
        yPos = y;
        size = s;
    
        fill(0, 255, 0);
        rect(xPos, yPos, size, size);
        fill(255);
      }
    }
    
    // The constructor has different values as parameters which can be used
    // with metods to create different shapes - color blue
    class ShapeWithMethod {
      int size;
      float xPos, yPos;
      float fixedX = 20;
      float fixedY = 50;
    
      ShapeWithMethod(float x, float y, int s) {
        xPos = x;
        yPos = y;
        size = s;
      }
    
      void displayRect() {
        fill(0, 0, 255);
        rect(xPos, yPos, size, size);
      }
    
      void displayCircle() {
        ellipse(xPos, yPos, size, size);
      }
    }
    

    The last class also uses functions(methods) that you can use along with a constructor. The constructor acts as "settings" for the shapes that you wish to draw. Functions often do specific jobs and return or set parameters.

  • I have a question to that.

    Is it considered bad style to change a property / var inside a class from the outside?

    E.g. in my last sketch above say m1.location.x++; outside the class?

    Because that is doner21's implicit question (I think), why that is not good style.

    why is that not good style?

    are all properties considered taboo?

    Thank you!

  • edited March 2014

    Formally in the Java world, following strict OOP recommended patterns, in order to access an object's field,
    we'd have to use an intermediary accessor method. :-@

    However, nested classes can't hide properties from its top-class. Neither a top-class can't hide its own from their nested classes!

    Since Processing is oriented to graphics sketches rather than business apps, getters & setters are likely a worthless nuisance!
    Even PVector class doesn't have such fancy accessors anyways! =))

    What I find bad programming is when an object tries to act outside its own class.
    Even directly accessing the data-structure which stores itself and its siblings! ~X(

  • Thanks a lot!

Sign In or Register to comment.