Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners memory allocation

Have Programming Question? Ask it here!
 
 
 


himanshu bhardwaj
memory allocation
2 Answer(s)      2 years and a month ago
Posted in : Java Beginners


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 ?


Advertisement
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 .



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
Java memory allocation  How does Java allocate stack and heap memory
Java Memory allocation - Java Interview Questions
Java Memory allocation  By default what is the memory allocation size for a single class? How the memory is allocated? Based on what procedure
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 memory
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
memory management
memory management  memory management in java
memory management
memory management  Why objects store in heap
Phantom memory
Phantom memory  What is phantom memory
Phantom memory
Phantom memory  hello,, What is phantom memory
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
Memory Management
Memory Management  how can we manage memory in iphone development???   hello, you can read all the memory management rules from http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt
Java memory leak
Java memory leak  What is memory leak
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
Main memory is also known as
Main memory is also known as   Main memory of a computer is also known
memory areas in jvm
memory areas in jvm  what are different memory areas in jvm?plz elaborate their significance
NSMutablestring memory leak
NSMutablestring memory leak  Why i am getting the nsmutablestring memory leak "NSCFString" leak
heap memory in j2me
heap memory in j2me  how to get j2me heap memory size
Memory - Java Beginners
Memory management in Java  Where are these Heap and Stack? Is there size fixed? what is core area in memory
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
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
Java memory optimization
Java memory optimization  When you think about optimization, what is the best way to find out the time/memory consuming process?  Hi, You can use memory dump and then analyze it to find the process/java code in your
memory span and shrink - pointers
memory span and shrink - pointers  Memory span and shrink as per application requirement and this is one of major advantage of pointer. please explain the above statment with an example with respect to c
UIImage imagenamed memory leak
UIImage imagenamed memory leak  Hi, Following code is creating memory leak: UIImage * img1 = [UIImage imageNamed:@"myimage.png"]; I have to replace existing image with new image. What could be the possible solution? Thanks
Memory leak in While loop
Memory leak in While loop  How to manage a memory leak in While loop ?   make sure loop is not running infinitley. ------------------------------------ Chandraprakash Sarathe http://javaved.blogspot.com/   
Does memory for Class or Object?
Does memory for Class or Object?   Does memory for class(methods & instance variables)allocated without it's object creation?If no,then The main() itself has associated with a class for which there is no instance creation,how
Java Array Initialization
Java Array Initialization       After declaring an array variable, memory is allocated to it. The "new" operator is used for the allocation of  memory
GeneralBlock Memory Leak
GeneralBlock Memory Leak   Hi all, I'm testing my iPhone application using instrument test in XCode 4... but on every test it throws a GeneralBlock... to debug this kind of memory leaks in iPhone instrument test
Out Of Memory Exception Erorr
Out Of Memory Exception Erorr  hello how are you?I hope you are fine I'm using jgrasp program my problem is in java programming my program compiled... understood that I have to increase the program memory but I don't know how?! *I gone
PC Memory Store
PC Memory Store       Great use of ajax for the memory configurator tool to find compatible memory for particular computer systems. Read full Description
received memory warning level 1
received memory warning level 1  received memory warning level 1 and if i continue to it .. i get second warning received memory warning level 2 And then my application get crash. Can any one please explain me what
What is In-Memory Computing?
and in-memory computing is another revolutionary steps that opens great array... proactive sectors and business purposes, in-memory computing or also commonly referred as in-memory database helps to decrease the complications in accessing
Java read file in memory
Java read file in memory In this section, you will learn how to read a file in memory. Description of code: The class FileInputStream get a channel...++)); } System.out.println(); } } The above code mapped the file in a memory
Java Memory Leak - Java Beginners
Java Memory Leak  I am having a memory leak in my java version of the game Snake. I have narrowed the exact step of when the program memory.... The actually object that's eating up my memory is an array of ints which grows
Java get Free Memory
Java get Free Memory       In this section, we are going to obtain the free memory. In order to get the memory left of being allocated, the method getruntime() returns
what type of computer memory is used for temporary storage
what type of computer memory is used for temporary storage  what type of computer memory is used for temporary storage   RAM or Random Access Memory is a computer memory that is used for temporary storage of data. RAM
Determining Memory Usage in Java - java tutorial
Determining Memory Usage in Java 2001-08-28 The Java Specialists' Newsletter [Issue 029] - Determining Memory Usage in Java Author: Dr. Heinz M. Kabutz... parts in Java is guessing how much memory is being used by your object. We
Java Get Memory
Java Get Memory       In this section of Java Example, you will see how to get the memory... can easily learn it. In this Java Get Memory Size example, we have created
Java file in memory
Java file in memory This section illustrates you how to map an entire file into memory for reading. Description of code: We used the FileChannel class along with the ByteBuffer class to perform memory-mapped IO for data of type byte
can a volatile memory loss data on power cut
can a volatile memory loss data on power cut  Can a volatile memory loss ache and main memory content on power cut?   In Volatile, Cache and main memory will lose their contents when the power is off
Finding memory consuming process - Java Beginners
Finding memory consuming process  Hi, While debugging or optimizing your java application, how to find the memory consuming processes? Thanks
ORA-01019: unable to allocate memory in the user side
ORA-01019: unable to allocate memory in the user side  when i trying to oracle i got the following error.. ORA-01019: unable to allocate memory in the user side... How to resolve in oracle
Java memory options from command line
Java memory options from command line  Hi, How to use java memory options from command line? Thanks   Hi, Here is the example code: java -Xms1056m -Xmx1056m MyProgram Thanks
Get Usage Memory Example
Get Usage Memory Example     ... in understanding Get Usage Memory. The class Memory Usage include the main method, that contain the following method -  !)Runtime.getRuntime( ).total Memory
 

 

 

DMCA.com