Java Virtual Machine Reading Assignment http java sun

  • Slides: 8
Download presentation
Java Virtual Machine • Reading Assignment: http: //java. sun. com/docs/books/vmspec/2 nd-edition/html/VMSpec. TOC. doc. html

Java Virtual Machine • Reading Assignment: http: //java. sun. com/docs/books/vmspec/2 nd-edition/html/VMSpec. TOC. doc. html Chapter 1: All Chapter 3: Sections 5, 6 Chapter 5: Sections 1 - 5 1

2. 3. 4. 5. • • • 1. 2. 3. 4. 5. static double

2. 3. 4. 5. • • • 1. 2. 3. 4. 5. static double rate = 3. 5; static int size = 3*(int)(Math. random()*5); . . . Class Initialization } Initialization of a class consists of two steps: – Initializing its direct superclass (if any and if not already initialized) – Executing its own initialization statements The above implies that, the first class that gets initialized is Object. Note that static final variables are not treated as class variables but as constants and are assigned their values at compilation. class Example 2{ static final int angle = 35; static final int length = angle * 2; . . . } 2

Class Instantiation • After a class is loaded, linked, and initialized, it is ready

Class Instantiation • After a class is loaded, linked, and initialized, it is ready for use. Its static fields and static methods can be used and it can be instantiated. • When a new class instance is created, memory is allocated for all its instance variables in the heap. • Memory is also allocated recursively for all the instance variables declared in its super class and all classes up in inheritance hierarchy. • All instance variables in the new object and those of its super classes are then initialized to their default values. • The constructor invoked in the instantiation is then processed according to the rules shown on the next page. • Finally, the reference to the newly created object is returned as the result. 3

Class Instantiation – Cont’d • Rules for processing a constructor: 1. Assign the arguments

Class Instantiation – Cont’d • Rules for processing a constructor: 1. Assign the arguments for the constructor to its parameter variables. 2. If this constructor begins with an explicit invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively. 3. If this constructor is for a class other than Object, then it will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively. 4. Initialize the instance variables for this class with their proper values. 5. Execute the rest of the body of this constructor. 4

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. Example of Class Instantiation 1 class Grand. Father{ int grandy = 70; public Grand. Father(int grandy}( this. grandy = grandy; System. out. println("Grandy: "+grandy; ( { { class Father extends Grand. Father} int father = 40; public Father(int grandy, int father}( super(grandy; ( this. father = father; System. out. println("Grandy: "+grandy+" Father: "+father; ( { { class Son extends Father} int son = 10; public Son(int grandy, int father, int son}( super(grandy, father; ( this. son = son; System. out. println("Grandy: "+grandy+" Father: "+father+" Son: "+son; ( { { public class Instantiation} public static void main(String[] args}( Son s = new Son(65, 35, 5; ( { { 5

 • Example of Class Instantiation 2 Calling method of a subclass before the

• Example of Class Instantiation 2 Calling method of a subclass before the subclass is initialized 1. class Super} 2. Super() { print. Three{ ; () 3. void print. Three} () 4. System. out. println("three; (" 5. { 6. { 7. class Test extends Super} 8. int three = (int)Math. PI; // That is, 3 9. public static void main(String[] args} ( 10. Test t = new Test; () 11. t. print. Three; () 12. { 13. void print. Three} () 14. System. out. println(three; ( 15. { 16. { 6

class Base} Example of Class Instantiation 3 Base}() 1. 2. 3. 4. 5. 6.

class Base} Example of Class Instantiation 3 Base}() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. this(102; ( System. out. println("Inside Base; ("() { Base(int x}( this("ICS ", x; ( System. out. println("Inside Base(int x; ("( { Base(String s, int x}( super; () System. out. println(s+x; ( { { public class Derived extends Base} public Derived}() this("ICS 201; (" System. out. println("From Derived; ("() { public Derived(String s}( System. out. println("From Derived(String s; ("( System. out. println(s; ( { public static void main(String g}([] new Derived; () { { 7

Exercises 1. Write a program to show that each loaded type has only one

Exercises 1. Write a program to show that each loaded type has only one instance of java. lang. Class associated with it, irrespective of how many times it is used in a class. 2. Write a program to show that Loading may be caused either by creating an instance of a class or by accessing a static member. 3. Write a class to show that class variables are initialized with their default values when loaded. Also show that instance variables are initialized with their default values when an object is created. 4. Demonstrate that Verification actually takes place in the Loading process. To do this, write a class, Base, and a class, Sub. Class, that extends Base. Compile both classes. Then modify Base by changing the signature of one of its methods and compile it alone. Now write a test program that uses Subclass and try to compile and run it. 8