We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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 instanceNote 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.
BTW the problem is not with Eclipse or Processing they work the same way, the Java way.
HTH
Class2(PApplet pa)
.0
:this(pa, 0);
.Class2(PApplet pa, int val)
.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
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. >-)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
. :-\"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()?
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:And that's exactly what I said.
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?
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
It doesn't work. It throws an error that it cannot find class sc. Thank you for the feedback.
Kf