• Post Reply Bookmark Topic Watch Topic
  • New Topic

about order of execution  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) constructors
2)static methods
3)static blocks
out of these three.....
can any one please till the order of memory allocation
and execution of code
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell us what you think, first.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Tell us what you think, first.


i think static methods fallowed by constructors and then static methods
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said "static methods" twice.

You are close though.

WP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:
You are close though.


Not really. And the "order of memory allocation" part of the question doesn't even make sense.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:You said "static methods" twice.

You are close though.

WP

sorry....

i think static methods fallowed by constructors and then staticblocks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajendar talatam wrote:
1) constructors


Invoked when you create an instance, totally independent of when #2 happens, or even if it ever happens at all.

2)static methods


Invoked when you invoke them, totally independent of when #1 happens, or even if it ever happens at all.

3)static blocks


Invoked when the class is initialized, which happens before #1 or #2 can happen.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is in the Java Language Specification. Also look at this section.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also write a small class with a static initializer block and a constructor and a static method that each print a message; run it and see in which order the messages are printed.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You could also write a small class with a static initializer block and a constructor and a static method that each print a message; run it and see in which order the messages are printed.

True, but looking it up in the specification is always better. For example, if the specification didn't prescribe a concrete order, I could came to a wrong conclusion based on a test case I tried.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!