UML - Java Final and Finally Exceptions UML Multiple...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
YHL UML 1 Java Final and Finally Exceptions UML Multiple Inheritance Sunday, October 23, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YHL UML 2 Java "final" create a constant: (15.18) interface A { public static final double PI = 3.14159; } class X { double x = A.PI } Sunday, October 23, 2011
Background image of page 2
YHL UML 3 Sunday, October 23, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YHL UML 4 Sunday, October 23, 2011
Background image of page 4
YHL UML 5 class zeta extends Math { . . . static double sqrt(double a) { System.out.println("called zeta.sqrt"); } } class Test { static void main(. . .) { Vector<Math> v = new Vector<Math>(100); for (. . .) { if (foo(i)) { v[i] = Class.forName("Math"); } else { v[i] = Class.forName("Zeta"); } v[i].sqrt( ); } } } Sunday, October 23, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YHL UML 6 constant parameter: (9.8) void foo (final int x) { x = 100; // error } block inheritance (class): (3.6) final class X { } class Y extends X { // error } Sunday, October 23, 2011
Background image of page 6
YHL UML 7 block overriding (method): (3.6) class X { final public void foo () { } } class Y extends X { public void foo () { } // error } Sunday, October 23, 2011
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YHL UML Other uses of final as a prefix to a qualifier or a keyword in Java 8 Sunday, October 23, 2011
Background image of page 8
YHL UML 9 Java "finally" exception handling: (10.5) try { . .. } catch (exception_type1 ide1) { . .. } catch (exception_type2 ide2) { . .. } finally { . .. // always execute, regardless of exceptions } Sunday, October 23, 2011
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YHL UML 10 Java “finalize” Called before an object’s memory is reclaimed by the garbage collector: (11.9) //GC.java class X { int id; static int nextId = 1; public X() { id = nextId++; } protected void finalize () throws Throwable { if ( id%1000 == 0 ) { System.out.println("Finalization of X object, id = " + id); } super.finalize (); } } Sunday, October 23, 2011
Background image of page 10
YHL UML 11 class Test { public static void main( String[] args ) { X[] xarray = new X[ 10000 ]; for (int i = 0; i < 10000; i++ ) xarray[i] = new X(); xarray = null; System.gc(); } } Sunday, October 23, 2011
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
YHL UML Some subtleties about finalize Consider a large array shared by many objects and referenced via a static class field When the last object using this storage ( o1 ) is garbage collected, we would like to free the storage pointed to by the static field. finalize kind of allows us to do this. When o1 is garbage collected, its finalize method is called in another thread , o1 is not garbage collected at that time, and the garbage collector proceeds to collect other garbage objects There are two problems: Objects are not always garbage collected (think generational collector) 12 Sunday, October 23, 2011
Background image of page 12
YHL UML Some subtleties about finalize There is a big problem: Objects are not always garbage collected (think generational collector If the issue is only reclamation of storage, then if the 4 gigabytes is needed the garbage collector will eventually be run on o1 and the program will function it may have poorer performance, however, because of the storage used by the object pointed to the by the static pointer and at this time we won't even worry about the situation
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2012 for the course ECE 462 taught by Professor Samuelmidkiff during the Fall '11 term at Purdue University-West Lafayette.

Page1 / 73

UML - Java Final and Finally Exceptions UML Multiple...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online