no-arg.Child.shouldgo.constRucted();
/* i was so excited that i finally understood something - i didn’t realize that this is really just code (or derivative of code) that i’ve been studying in my OCA book.
(while im remembering stuff - the oca/ocp book - K&B book has a really interesting code snippet along these same lines -ala- import java.awt.Dimension (not - but it is - pg 134) & an anon-array - passing elements randomly)
Important point for me to remember is that i wondered why do u even include - this() - what is it doing?
what is the reason they even have that there? - sort of like calling super() to the Object parent-class - its - implicit anyway- and seems like its just there to take up space - i really really didn’t get it at all - but -
finally - i understood that values can be passed from: this(value) - to super(value) - and eventually affect fields - even private fields - of a parent or super-parent class - & upon re-reading see that they don’t make this particular point at all - it’s just something between me & my friends jvm & the compiler */
/* aka: _2write2privateFields(); */
/* without using getters & setters - using constructors & this() instead: */
import static java.lang.System.out;
public class Parent {
private int writeToItAnyway; /* private ivar - not visible to code in Child class */ public Parent(int constRvalue) { this.writeToItAnyway = constRvalue; /* u can set value of private variable in the Parent class by passing a value to its constructor */
print("Parent constR1 constRvalue is: " + constRvalue); print("writeToItAnyway = " + writeToItAnyway); } public static void main(String[] array){ new Child(); } //custom print method: static void print(Object obj){ out.println(obj); } } class Child extends Parent { public Child(int x){ super(x); /*pass a value to the Parent constructor*/ print("A copy of the fixed value: 900 is passed here: " + x); } public Child(){ this(900); /*no-arg constR passes - 900 to its own constR1 */ } }
// OUTPUT:
Parent constR1 constRvalue is: 900 writeToItAnyway = 900 A copy of the fixed value: 900 is passed here: 900
/* alternate Child no-arg constR code: */
public Child() {
this((int)(Math.random()*901)); }
/* so: basically - the value specified in “ this() “ gets sent from the Child constructor - all the way up the call stack - until it reaches the Parent constructor & then the value gets set as the ivar value - to keep ivar private - u don’t let the constructor write to the variable: this.ivar= arg | also: this keyword ( this.ivar ) is unnecessary bc var names r different - but - this keyword makes the code easier to read/understand */















