1 / 5

SEEM3460 Tutorial

SEEM3460 Tutorial. Java Keyword – static. 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

earl
Download Presentation

SEEM3460 Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SEEM3460 Tutorial Java Keyword – static

  2. static Variables class XY { staticintx; // class variable inty;// instance variable } • XY xy1 = new XY(); • XY xy2 = newXY(); • 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 { staticintx() {…}; // class method inty() {…};// instance-bounded method } • XY xy1 = new XY(); • XY xy2 = newXY(); • 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 { staticclass X {…}; class Y {…}; } • X is a classnested 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 introundedTotalCost(); • introundedCost(); • Update the constructor for making use of the new variables

More Related