Presentation is loading. Please wait.

Presentation is loading. Please wait.

There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program.

Similar presentations


Presentation on theme: "There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program."— Presentation transcript:

1 There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program completes. Automatic Data The data is created when a method begins to execute and is deallocated when the method returns. Dynamic Data The data is created and deallocated under program control. Recall that reference data has two parts:  the reference (address) and  the object In Java a garbage collector recovers any unused dynamic data. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 Example public class Point { private int x; private int y; private static int statInt=0; public Point(int a, int b) { x = a; y = b; statInt++; } public int getX() { return x; } public int getY() { return y; } public int getStatInt() { return statInt; } public class Pointless { private Point pt; private static Point statPt; public Pointless() { pt = new Point(111, 222); statPt = new Point(33, 44); } public void slide(int dx) { pt= new Point(pt.getX()+dx, pt.getY()); } public void assignToStat(Point p) { statPt = pt; pt = p; } public Point getPt() { return pt; } public Point getStatPt() { return statPt; } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 trace the following... public class go { public static void main(String args[]) { Point p1, p2, p3; p1 = new Point(10, 20); p2 = new Point(30, 30); System.out.println( p1.getStatInt() ); p3 = new Point(p1.getStatInt(), p2.getStatInt()); Pointless ptless1, ptless2; ptless1 = new Pointless(); ptless2 = new Pointless(); ptless1.slide(3); ptless1.assignToStat( p1 ); ptless2.assignToStat( new Point(3, 4) ); System.out.println( ptless1.getPt().getX() ); System.out.println( ptless2.getStatPt().getX() ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

4 trace the following by checking out memory allocation... public class Driver { private int intVar = 1; public Driver() { aMethod(2); System.out.println(StatThing.statVar); StatThing thing1 = new StatThing(3, 4); StatThing thing2 = new StatThing(5, 6); } private void aMethod(int j) { int localVar = j; System.out.println(localVar); } public static void main(String args[]) { Driver d = new Driver(); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub. public class StatThing { private int intVar = 100; public static int statVar = 200; public StatThing(int k, int n) { intVar = intVar + k; statVar = statVar + 200; System.out.println(intVar); System.out.println(statVar); }

5 public class IntCell { public int content; public IntCell link; public intCell(int a) { content = a; link = null; } public void setLink(IntCell c) { link = c; } public int setContent(int j) { content = j; } public okay for this example /* in some other class... IntCell cell1, cell2; cell1 = new IntCell(2); cell2 = new IntCell(3); cell2.setLink( cell1 ); The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

6 A More Extensive Example IntCell cell1 = new IntCell(11); IntCell cell2 = new IntCell(22); IntCell cell3 = new IntCell(33); cell2.setLink( cell1 ); cell1.setLink( cell3 ); cell1 = null; cell3 = null; System.out.println( cell2.content ); System.out.println( cell2.link.content ); System.out.println( cell2.link.link.content ); cell2.link.setContent(44); cell2.link.link.setContent(55); cell2.link.link.setLink(cell2); System.out.println( cell2.link.link.link.content ); The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt "There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program."

Similar presentations


Ads by Google