This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes neebie's question about the execution order in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "neebie Watch "neebie New topic
Author

neebie's question about the execution order in java

SkyGriller tian
Greenhorn

Joined: Jan 27, 2012
Posts: 8


My question: doesn't the codes execute from top to bottom? if so, "public static void passMethod(int p){p=10}" should be a head of "public static void main..." to give some kind of meaning to "passMethod".
does the execution order(from top to bottom) only aplly within a block between "{"and "}" ?


much thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
SkyGriller tian wrote:

My question: doesn't the codes execute from top to bottom?
No. The code onoly executes from top to bottom when it is in a sequential situation
if so, "public static void passMethod(int p){p=10}" should be a head of "public static void main..." to give some kind of meaning to "passMethod".
does the execution order(from top to bottom) only aplly within a block between "{"and "}" ?


much thanks!
No, it is more complicated than that. You are in sequential mode inside a block, but that block may be interrupted by selection and repetition, like thisYes, the bits inside each {} are in top-to-bottom order, but the if and while can change that order. There is a whole chapter in the Java Language Specification about that, but that is not at all easy to read.

Do you mean, “Doesn’t the code have to be declared in a particular order for the javac tool to compile it?” If so, the answer is definitely no.
SkyGriller tian
Greenhorn

Joined: Jan 27, 2012
Posts: 8

sorry. i don't think i made it clear enough. after a while of thinking, my question should be like this:in my code, there are no Control Flow Statement, so it's executed from top to bottom. Then, THE METHOD "passMethod()" is invoked before the METHOD IS DECLARED. But I don't seem to get an error caution. MUCH THANKS!
PS:still grateful for the answers above!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

SkyGriller tian wrote:
sorry. i don't think i made it clear enough. after a while of thinking, my question should be like this:in my code, there are no Control Flow Statement, so it's executed from top to bottom. Then, THE METHOD "passMethod()" is invoked before the METHOD IS DECLARED. But I don't seem to get an error caution.

The order methods are declared in doesn't matter. As long as the compiler can find the method, it's smart enough to cope.

I've no idea how the compiler actually does this in practice, but I could think of one approach that would work. The compiler could take one pass through all the code just working out what methods (and instance variables) there were. Then it could take a second pass where it actually compiles each method. By this point it already knows all the methods that exist, and so knows what can be called. I suspect what actually happens is even cleverer, but it's one way to think about it.

And welcome to The Ranch!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
It would appear I did understand the question.

Another way to implement such method finding is for the tool to maintain a Set of method objects, field objects, etc., which are called and which are compiled. At the end of the class one can compare the sets of requirements and actual members available and if there is any mismatch, e.g. no member, wrong argument or return type, throw an error. That would obviate the need for a second pass.
A lot of compilers (e.g. most of those I have seen for C) cannot find functions unless they are declared in a particular order, but Java™ isn’t C, and behaves differently.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Campbell Ritchie wrote:It would appear I did understand the question.

Another way to implement such method finding is for the tool to maintain a Set of method objects, field objects, etc., which are called and which are compiled. At the end of the class one can compare the sets of requirements and actual members available and if there is any mismatch, e.g. no member, wrong argument or return type, throw an error. That would obviate the need for a second pass.
A lot of compilers (e.g. most of those I have seen for C) cannot find functions unless they are declared in a particular order, but Java™ isn’t C, and behaves differently.


are you including function prototypes as being "declared"? as yes it has to have heard about it before trying to call it.
SkyGriller tian
Greenhorn

Joined: Jan 27, 2012
Posts: 8
thanks all above. my question is now crushed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Yes, a function prototype would count as being declared in C.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
SkyGriller tian wrote:thanks all above. my question is now crushed.
You’re welcome

Anybody is still permitted to add anything useful to this thread.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

if i recall my compilers class correctly, there are really three things that need to happen...

1) compilation
2) linking
3) execution

broadly speaking, the compile stage checks syntax and converts the code from one language to another. For Java, it converts you .java file to a .class file. You can compile a class that calls methods that are not defined anywhere yet...you know what the method should look like, but you don't have to know where it is just yet.

The link stage is the part that matches all the method calls to method declaration. This is where you get into trouble if you call a method that has never been defined anywhere. So, what i THINK happens is what we call the java compiler would compile all the methods first. Once they are all compiled, it goes through and links them together.

That's why it doesn't matter what order the methods are in the file...they all get compiled before they are linked, regardless.

Again, it's been 15 years since I had that class, and I'm really NOT a java expert on such things..but that's my guess.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: neebie's question about the execution order in java