1

This is the code:

    import acm.program.*; 

    public class test extends GraphicsProgram{ 

        public test(){ 

           println(getHeight()); 

        } 

        public void run(){ 

          println(getHeight()); 

        } 

    }

The executed result is 0 472. Why does getHeight() in the constructor return 0, whereas run() returns 472, which is the real value?

Robin Green
  • 32,079
  • 16
  • 104
  • 187

2 Answers2

6

The height has not been set until the init() method, which executes before the run() method.

Marcelo
  • 11,218
  • 1
  • 37
  • 51
  • What if I have to call getHeight() method without having run() method, e.g: the class **test** is used as a subclass, which not necessarily has a run() method; and main class is gonna create an object of this class **test** with getHeight() method actually functioning. – Robin Banner Jun 20 '11 at 17:04
  • Override the `init()` method of the subclass, making sure to call `super.init()` before trying to access the `getHeight()` method. – Marcelo Jun 20 '11 at 17:06
  • Bear with me but I failed again.. here is my code: 1)(class to be used)`import acm.graphics.*; public class test extends GCanvas{ public int x; public void init(){ x = getHeight(); } } ` 2)(main executive class)`import acm.program.*; public class test2 extends ConsoleProgram{ public void run(){ test a = new test(); a.init(); println(a.x); } }` which still returns a zero.... – Robin Banner Jun 20 '11 at 17:53
  • You are extending a different class now, but basically where you have `public void init(){ x = getHeight(); }` call `public void init(){ super.init(); x = getHeight(); }` – Marcelo Jun 20 '11 at 18:00
  • Thanks for your answer, but this really drives me crazy... when i try to add the line `super.init();` Eclipse says that init() is undefined for the type GCanvas.. so I change the GCanvas back to GraphicsProgram. The warning from Eclipse goes away, but when I run the program test2, getHeight() still returns a zero.. – Robin Banner Jun 20 '11 at 18:16
  • Sorry that I couldn´t help you, the only other thing I would suggest is to use a debugger and some watches to see when the height is getting initialized. – Marcelo Jun 21 '11 at 12:18
0

An item doesn't have a height at first. Most likely you are calling getHeight() before the component is laid out or given a height.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • What if I have to call getHeight() method without having run() method, e.g: the class **test** is used as a subclass, which not necessarily has a run() method; and main class is gonna create an object of this class **test** with getHeight() method actually functioning. – Robin Banner Jun 20 '11 at 17:04
  • A subclass of test will always have a run method because test has a run method. Either way, something will need to set the height before you can access the value. – jzd Jun 20 '11 at 17:26
  • sorry for using the misleading word subclass .. bear with a starter .. what I really mean is another class, named main_class(with run method), which do not have the hierarchical relationship. The *main_class* is gonna create an object of the class *test*(without run method). What to do if I want getHeight() in *test* class take its function. – Robin Banner Jun 20 '11 at 18:04
  • Both examples are completely unrelated to the core problem. I don't think you understand what the code is actually doing. – jzd Jun 20 '11 at 19:29