Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();

Similar presentations


Presentation on theme: "SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();"— Presentation transcript:

1 SEEM3460 Tutorial Java Keyword – static

2 static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY(); XY xy2 = new XY(); XY.x, xy1.x, xy2.x all refer to same int using XY.y gives an error xy1.y and xy2.y refer to different int

3 static Methods class XY { static int x() {…}; // class method int y() {…}; // instance-bounded method } XY xy1 = new XY(); XY xy2 = new XY(); XY.x(), xy1.x(), xy2.x() are the same calling XY.y() gives an error xy1.y() and xy2.y() calls the same methods but with different scopes (encapsulation) keyword this can be used in y, but not x

4 static Classes (Advanced, FYI) class XY { static class X {…}; class Y {…}; } X is a class nested in XY X can be used like a normal class of the name XY.X Y is a Inner class of Outer class XY Y cannot be used without an instance of XY each instance of Y must be attached to an instance of XY, the containing instance of XY can be referred by XY.this

5 Exercise Based on CD create: static double totalCost; static CD mostExpensiveCD; double Cost; // this already exists static int roundedTotalCost(); int roundedCost(); Update the constructor for making use of the new variables


Download ppt "SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();"

Similar presentations


Ads by Google