Troubles with Processing inside Eclipse

Hello everyone! Could someone please help me with an issue i have? So i followed every step on how to make multiple classes and everything works fine except for constructors. Lets say i am in my MainClass calling

SecondClass sc = new SecondClass (25);

And then inside SecondClass i have constructor

PApplet parent;
int x; //we declare
SecondClass(int vars){
    x = vars //here if i print this value inside of this constructor everything is fine in console i get 25
}
SecondClass(PApplet p){
    parent = p;
}
public void nullx(){
    System.out.print(x); // here console gives me 0 and i dont understand why
}

Please help me.

Answers

  • It doesn't make sense to have such multiple constructors that don't initialise all variables.
    Is the second class a seperate Java file? Is it public/private/protected? Why is the constructor not public?

  • Yes it is the seperate file. Why should it be public? I just wonder why x inside of consturctor has the right value and then outside constructor it has value 0 or it is null. It messes up if i want to make objects this way because values arent actualy set or something and then i get nullpointer or value 0 or null

  • edited December 2016 Answer ✓

    The class is a blueprint for creating objects. The constructor is a special function instantiating (i.e. making) the object, it can only be called once on an object for instance

    SecondClass sc;
    sc = new SecondClass (25); // creates an object and initialise 'x' to 25
    sc = new SecondClass(this); // creates a NEW object and initialises the field 'p'
    

    Note that in line 2 we create an object but it sets the value for x but NOT P. In the next line we create a second object which initialises P but not x. This second object replaces the first object which will be deleted by the garbage collector.

    The solution is to have a single constructor that initialises both fields like this.

    public void setup() {
      SecondClass sc = new SecondClass (this, 25);
    }
    
    
    // And then inside SecondClass i have constructor
    class SecondClass {
      PApplet parent;
      int x; //we declare
    
      SecondClass(PApplet p, int vars) {
        parent = p;
        x = vars; 
      }
    
      public void nullx() {
        System.out.print(x); // here console gives me 0 and i dont understand why
      }
    }
    

    BTW the problem is not with Eclipse or Processing they work the same way, the Java way.

    HTH

  • edited December 2016
    • Another approach is to incrementally overload the constructors to include more parameters.
    • Let's start off w/ the minimum: Your class seems to need a valid PApplet in order to work.
    • So we already got constructor's 1st signature: Class2(PApplet pa).
    • We can then choose some arbitrary default value for field x. For example 0: this(pa, 0);.
    • Now finally the fully signature w/ 2 parameters: Class2(PApplet pa, int val).
    • In this case, the caller choose a specific initial value for x rather than leave it as its default. :-bd

    /**
     * Constructor Overloading (v1.0)
     * GoToLoop (2016-Dec-17)
     *
     * forum.Processing.org/two/discussion/19794/
     * troubles-with-processing-inside-eclipse#Item_4
     */
    
    import processing.core.PApplet;
    
    public class Class2 {
      final PApplet p;
      int x;
    
      public Class2(PApplet pa) {
        this(pa, 0);
      }
    
      public Class2(PApplet pa, int val) {
        p = pa;
        x = val;
      }
    
      @ Override public String toString() {
        return PApplet.str(x);
      }
    }
    
    public Class2 sc;
    
    public void setup() {
      sc = new Class2(this);
      println(sc); // 0
    
      sc = new Class2(this, -5);
      println(sc); // -5
    
      exit();
    }
    
  • Thanks for the anwser :)

  • Based on the last post, is it possible for sc to access the parent's toString() function? In C++ I would perform a class casting. Is there anything similar in java? I tried:

    Object sc2 = new Class2(this,999);

    and or course, the answer was 999.

    Kf

  • edited December 2016

    Maybe there can be some reflect hack for it hidden deep somewhere within Java's API. :ar!
    But AFAIK, we can't access the original Object::toString() once it's been overridden by a subclass. =;

    Java's (cast) operator merely restricts which members are accessible for an object.
    But it doesn't change its actual datatype. Overridden members continue to be so even under a downcast.

    Only exception are static members.
    In those cases, current (cast) datatype determines which version we access. >-)

  • edited December 2016

    Of course, there's keyword super, which allows a class to access their immediate inherited type members in their original non-overridden implementation. B-)

    However, super is used within a class' block.
    Once an object is created from it, I don't think we can hack it w/ super. :-\"

  • edited December 2016

    Or maybe we can, if can can change the class that overrides the inherited method from its superclass.
    We can add an extra method to the class, one that calls super.methodThatWasOverriden() and returns the same.

    P.S. Will sc.super.toString() work? I doubt it will.
    P.S.2 Why do you use PApplet.str() on line 25? Why not Integer.toString()?

  • edited December 2016

    The only way I know about on how to get the original Object::toString() is by directly altering Class2 class and use keyword super there:

    /**
     * Constructor Overloading (v1.1)
     * GoToLoop (2016-Dec-17)
     *
     * forum.Processing.org/two/discussion/19794/
     * troubles-with-processing-inside-eclipse#Item_10
     */
    
    import processing.core.PApplet;
    
    public class Class2 {
      final PApplet p;
      int x;
    
      public Class2(PApplet pa) {
        this(pa, 0);
      }
    
      public Class2(PApplet pa, int val) {
        p = pa;
        x = val;
      }
    
      public String originalToString() {
        return super.toString();
      }
    
      @ Override public String toString() {
        return PApplet.str(x);
      }
    }
    
    public Class2 sc;
    
    public void setup() {
      sc = new Class2(this);
      println(sc, sc.originalToString()); // 0
    
      sc = new Class2(this, 3);
      println(sc, sc.originalToString()); // 3
    
      exit();
    }
    
  • And that's exactly what I said.

  • edited December 2016

    Why do you use PApplet.str() on line 25? Why not Integer.toString()?

    Usually I choose Processing's API over a pure Java solution. ~O)
    Mostly b/c it's easier to convert it to Pjs, p5.js and other "flavors" later on. :ar!

  • Oh, so Pjs doesn't covert Java methods to corresponding JavaScript methods? Or is my whole idea of how Pjs works wrong?

  • edited December 2016

    Pjs got a very limited Java set it is able to convert to JS.
    1st & most important rule is to stick to Pjs' API and pray: http://ProcessingJS.org/reference/ [-O<

  • Thxs for the example @GoToLoop.

    @Lord_of_the_Galaxy

    sc.super.toString()

    It doesn't work. It throws an error that it cannot find class sc. Thank you for the feedback.

    Kf

Sign In or Register to comment.