• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Jave Exercises - Code in Class Calls Itself?  RSS feed

 
Sterling Crapser
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing the exercises and so far have done quite well. If this were a college program I would say I'm averaging an A- on the problems. I've seen two code examples now where the code inside the class object is calling itself. I've never seen this before (I've been working with PowerBuilder since 1997). In my experience, functions and events are always set up to call other functions and events. Perhaps it can be done there too but I have never had reason to do so.

Can someone explain this? Maybe the book does later on but if so I'm not there yet.

Also, I've seen a class object where the main method calls a secondary method before the secondary method is defined (at least that's how the code is written). Is this possible because nothing executes until the code is compiled? I tend to think in linear terms which I know can cause problems. I just want to be sure.

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be easier to understand what you are trying to explain by posting the code of the class. Some code calling itself ? Do you mean recursive calls ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing is executed until the code is *executed* (not compiled as you stated above). The order of definition is irrelevent from a technical standpoint (but might matter for organizational or aesthetic reasons).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
recursion is a common technique in programming. The classic example is something like calculating the factorial of a number. Say you want to figure out what 10! is. You can take two approaches...

write a loop from 1 to 10 that multiplies the current index by the last product:



or you can think of it this way...you know that 10! is really the same as 10 * 9!. So all you have to do is figure out 9!. but 9! is really 9 * 8!. etc. so you write a method that breaks the original problem down into something simpler. the method also handles some 'base case' - i.e. some case that is so trivial, you break the recursive call. In this case, 1! is just 1, so we don't need any more calls to itself.

I think this is write - I wrote it off the cuff - didn't test
So if you pass in 10, it's going to calculate 10 * (factorial(9)). that then becomes 10 * 9 * (factorial(8))...etc.


 
Sterling Crapser
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies so far. Some of it is a bit over my head at the moment.

Here is a code example for this discussion taken from the book.

class HeapQuiz {
int id = 0;
public static void main(String [] args) {
int = 0;
HeapQuiz [] hq = new HeapQuiz[5];
while (x < 3) {
hq[x] = new HeapQuiz();
hq[x].id = x;
x = x + 1;
}
hq[3] = hq[1];
hq[4] = hq[1];
hq[3] = null;
hq[4] = hq[0];
hq[0] = hq[3];
hq[3] = hq[2];
hq[2] = hq[0];
}
}


After reviewing this code again, I think my main focus is on the line (in red) where the HeapQuiz object is being assigned to the array in the "while" loop. Isn't that the class object which all the code is defining? Sorry about no indentaion. For some reason I cannot get it to work in this post.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indentation only works with the Code Button. You would have to use the preview button, and then quote the line number.

What class object? There are objects there, but no class objects visible. A class object would be an object of the Class class. What you are doing is creating objects of the HeapQuiz class, then putting (references to) them into that array. Note you then swap them all around, so it takes lots of thought to work out what the final result is.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sterling Crapser wrote:Thanks for the replies so far. Some of it is a bit over my head at the moment.

Here is a code example for this discussion taken from the book.



After reviewing this code again, I think my main focus is on the line (in red) where the HeapQuiz object is being assigned to the array in the "while" loop. Isn't that the class object which all the code is defining? Sorry about no indentaion. For some reason I cannot get it to work in this post.


I think I understand where you are confused. A class can create a new object of itself within its own code. What is happening is the JVM is calling the public static void main(String args[]) method. From there an array of HeapQuiz objects is created, with 6 currently null references (6 because arrays start at 0). The while loop mearly creates a new HeapQuiz object and places a reference to it in the array. It won't call any code that is in that object, just create it (well, technically it calls constructors, but that is a little ahead of what this topic is about). The rest of the code is just assigning references to different elements of the array, which is simple enough to sort out with pen and paper.
 
Sterling Crapser
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah...thanks. That does make sense now. Each object is independent and the code is essentially creating clones of itself, assigning each one to the array. But as you say, the code within those objects is not being executed.

This book is very good. That exercise is to figure out what the value of each array element will be once it finishes executing. I got the exercise correct but this code threw me.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no cloning going on. The same object is added at several places in the array. You will have to go through the whole thing with pencil and paper to have any idea of what is going on.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!