memory allocation

memory allocation

class Bird {
{ System.out.print("b1 "); }
public Bird() { System.out.print("b2 "); }
}
class Raptor extends Bird {
static { System.out.print("r1 "); }
public Raptor() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Hawk();
System.out.println("hawk ");
}   
}

What is sequence of execution of statement ? memory allocation ?

View Answers

July 5, 2012 at 5:16 PM

The code uses static and instance init blocks. Static init blocks are executed only once the first time the class is loaded. Instance init blocks are executed every time a new instance of the class (object) is created, just after the call to super() from any subclasses constructors. Based on that fact, we can see that when the program is run, the class Hawk is loaded, as its super class Raptor, and the super-super class Bird. In the case of Raptor, there are two static init blocks, and they are executed in the order in which they appear in the class file. So, output begins with: r1 r4

There are no other static init blocks so the next thing that happens is back in the main method where we have the:

r1 r4 Pre

Next, we create an instance of our Hawk class. When this object is created, the non-static init blocks are executed. They are executed just after the call to the super() constructor, so the first one that will be executed is in the top of the object tree, and we get b1, and the the super-super constructor runs and we get b2.

r1 r4 Pre b1 b2

In the Raptor class, the non static-init block runs followed by the constructor in the Raptor class runs, so we get r3 r2. Finally, we end with the System.out.println in the last section that displays "hawk ".

The final result is:

r1 r4 Pre b1 b2 r3 r2 hawk


July 5, 2012 at 10:03 PM

what are the instance init block.? can you give me an example illustrating when the init block are called every time the new instance of the class is created. instance is something like if demo is a class demo d1 or new demo(). Thank you .

Ads









Related Tutorials/Questions & Answers:
memory allocation
memory allocation   class Bird { { System.out.print("b1 "); } public Bird() { System.out.print("b2 "); } } class Raptor extends Bird { static... of execution of statement ? memory allocation ?  The code uses static
memory allocation
memory allocation  If we assign memory malloc(5) and access more than the assigned memory then what will happen? 1.segmentation error 2.memory leak 3.program will not compile 4.Your view
Advertisements
Memory Allocation
Memory Allocation  What Is The Order Of Execution Of Statement 1) Statement Enclosed in {}. 2) Static 3) Public Static Void Main()   When jvm loads a class, static block is executed first, then main method. And at last
java memory allocation for member variables and member functions
java memory allocation for member variables and member functions  ... an object q of test class then when shall the memory be allotted for function fun...() that has three variables a=5,b=6.My questions are 1>When will the memory be allotted
Controlling Java Heap Size Memory Allocation
Controlling Java Heap Size Memory Allocation  Hi, Tell me about Controlling Java Heap Size Memory Allocation? I have to run java program from console and allocate 2GB of RAM to Java Heap Size. Thanks   Hi, You can
Memory
Memory   what is the different between heap memory and stack memory ? please explain with example
memory
memory   hi i am jane pls explain the difference between heap memory and stack memory
yarn.scheduler.minimum-allocation-mb
yarn.scheduler.minimum-allocation-mb  Hi, I am submitting an Java... as it outside max allowed. yarn.scheduler.minimum-allocation-mb How to solve... it: --driver-memory 1665m \ --driver-cores 2 \ --executor-memory 4g thanks
memory management
memory management  memory management in java
ModuleNotFoundError: No module named 'Asset_Allocation'
ModuleNotFoundError: No module named 'Asset_Allocation'  Hi, My... named 'Asset_Allocation' How to remove the ModuleNotFoundError: No module named 'Asset_Allocation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'Asset_Allocation'
ModuleNotFoundError: No module named 'Asset_Allocation'  Hi, My... named 'Asset_Allocation' How to remove the ModuleNotFoundError: No module named 'Asset_Allocation' error? Thanks   Hi, In your
memory management
memory management  Why objects store in heap
memory layout
memory layout  how much space does each segement(data,stack,heap,static) holds in memory? memory here refers to ram or hard disk
heap memory
heap memory  how long is the object stored in heap memory persist
ModuleNotFoundError: No module named 'rig-cpp-key-allocation'
ModuleNotFoundError: No module named 'rig-cpp-key-allocation'  Hi...: No module named 'rig-cpp-key-allocation' How to remove the ModuleNotFoundError: No module named 'rig-cpp-key-allocation' error? Thanks   Hi
ModuleNotFoundError: No module named 'asset_allocation_inputs'
ModuleNotFoundError: No module named 'asset_allocation_inputs'  Hi...: No module named 'asset_allocation_inputs' How to remove the ModuleNotFoundError: No module named 'asset_allocation_inputs' error? Thanks   
ModuleNotFoundError: No module named 'asset_allocation_inputs'
ModuleNotFoundError: No module named 'asset_allocation_inputs'  Hi...: No module named 'asset_allocation_inputs' How to remove the ModuleNotFoundError: No module named 'asset_allocation_inputs' error? Thanks   
ModuleNotFoundError: No module named 'beancount-portfolio-allocation'
ModuleNotFoundError: No module named 'beancount-portfolio-allocation' ...: No module named 'beancount-portfolio-allocation' How to remove the ModuleNotFoundError: No module named 'beancount-portfolio-allocation' error
ModuleNotFoundError: No module named 'latent-dirichlet-allocation'
ModuleNotFoundError: No module named 'latent-dirichlet-allocation'  ...: No module named 'latent-dirichlet-allocation' How to remove the ModuleNotFoundError: No module named 'latent-dirichlet-allocation' error? Thanks
ModuleNotFoundError: No module named 'latent-dirichlet-allocation'
ModuleNotFoundError: No module named 'latent-dirichlet-allocation'  ...: No module named 'latent-dirichlet-allocation' How to remove the ModuleNotFoundError: No module named 'latent-dirichlet-allocation' error? Thanks
ModuleNotFoundError: No module named 'rtc-tools-water-allocation'
ModuleNotFoundError: No module named 'rtc-tools-water-allocation'  Hi...: No module named 'rtc-tools-water-allocation' How to remove the ModuleNotFoundError: No module named 'rtc-tools-water-allocation' error? Thanks
Java memory leak
Java memory leak  What is memory leak
There is insufficient memory for the Java Runtime Environment to continue.
allocation (mmap) failed to map 1073741824 bytes for committing reserved memory...There is insufficient memory for the Java Runtime Environment to continue.  Hi, I am getting error "There is insufficient memory for the Java
Creating a memory leak with Java
Creating a memory leak with Java  Creating a memory leak with Java
heap and stack memory
the difference between heap memory and stack memory   STACK memory is referred as temporary memory,if you come out of the program the memory of the variable will not no more there.[eg., int a; memory for a will not maintained
NSMutablestring memory leak
NSMutablestring memory leak  Why i am getting the nsmutablestring memory leak "NSCFString" leak
memory areas in jvm
memory areas in jvm  what are different memory areas in jvm?plz elaborate their significance
Main memory is also known as
Main memory is also known as   Main memory of a computer is also known
ModuleNotFoundError: No module named 'memory'
ModuleNotFoundError: No module named 'memory'  Hi, My Python... 'memory' How to remove the ModuleNotFoundError: No module named 'memory'... to install padas library. You can install memory python with following command
heap and stack memory
heap and stack memory   hi i am jane pls explain the difference between heap memory and stack memory with example programs
iphone memory management
iphone memory management  I am confused with the memory management issue in iPhone. I do not know which variable need to release

Ads