slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Static variables and methods PowerPoint Presentation
Download Presentation
Static variables and methods

Loading in 2 Seconds...

  share
play fullscreen
1 / 33
Download Presentation

Static variables and methods - PowerPoint PPT Presentation

ermin
124 Views
Download Presentation

Static variables and methods

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Static variables and methods Static variables are class variables used when we want all instances of a class to share data. Static variables store variables in common memory locations. Static methods are associated with the class and can be called even without creating an instance of the class. They can be invoked as classname.methodname

  2. Example Class Circle{ double radius; static int numberoOfObjects=0; Circle(){ radius=1.0; numberoOfObjects++; } Circle(double newRadius){ radius=newRadius; numberoOfObjects++; } static int getNumberOfObjects(){ return numberoOfObjects; } double getArea(){ return radius*radius*Math.PI; } }

  3. Example public class TestCircle{ public static void main(String[] args){ Circle c1= new Circle(); System.out.println(c1.radius+” “ +c1.numberOfObjects); Circle c2= new Circle(5); c1.radius=9; System.out.println(c1.radius+” “+ c1.numberOfObjects); System.out.println(c2.radius+” “ c2.numberOfObjects); Circle c3= new Circle(4); System.out.println(Circle.getNumberOfObjects()); }

  4. Static variables and methods can be used from instance or static method in the class. However, instance variables and methods can be used only from instance methods, not from static methods. public class A{ int i=5; static int k=2; public static void main(String[] args){ int j=i; //wrong, i is an instance variable. m1(); //wrong, m1 is an instance method. } public void m1(){ i=i+k+m2(I,k); } public static int m2(int I, int j){ return(int)(Math.pow(I,j)); } } Note: Declare a method to be static if it does not depend on an instance, e.g., public static int factorial(int n)

  5. Visibility modifiers: public, private, package-private • public makes classes, methods, data accessible from any class. • private makes classes, methods, data accessible from within its own class. • package-private (default) makes classes, methods, data accessible from within its own package. package packagename; Note: public/private should be used only with class members (not on local variables of method)

  6. Example: package p1; public class C1{ public int x;int y;private int z); public void m1(){ ….} void m2(){……} private void m3(){….} } public class C2{ void aMethod(){ C1 o= new C1(); //can access o.x, o.y, can invoke o.m1(),o.m2() //can not access o.z or invoke o.m3 } package p2; public class C3{ void aMethod(){ C1 o= new C1(); //can access o.x, can invoke o.m1() //can not access o.y, o.z or invoke o.m2, o.m3 }

  7. Examples of class: Recursive data structures

  8. Recursive data structures • Refers to a “connected” set of similar objects • Object A “connects” or “refers” to object B • For example, in a list of integers you can imagine each integer in the list as an object and each object connects to its next object thereby forming a list • The structure is called recursive because an object of type class A connects to another object of the same class type • Very important to build complex connected structures e.g., a network of cities where a “connection” could mean rail or road

  9. List structure • Why not just an array? • Basic operations needed on a list: search, insertion, deletion • All these are time consuming in arrays e.g., searching is O(n), insertion at head is O(n), and deletion from head is also O(n) • With lists, arbitrary insertion and deletion can be made in O(1) time; searching is still costly • Insertion and deletion involve changing a few references (independent of n)

  10. List structure • Consider insertion sort • For each element, we compared it with all elements on its left until a comparison failed and then shifted up all the elements on the right to make a hole for insertion • If the numbers are stored in a list, the last shift-up step can be completely avoided • Just insert one element and delete another: O(1) • with a reverse-sorted array, the number of comparisons is minimum while with a sorted array, the number of comparisons is maximum • But former spends more time in shift-up leading to both cases requiring roughly the same amount of time • With a list implementation, the first case will indeed be the fastest • Program = Algorithm + Data structure

  11. List structure public class IntegerList { private int data; private IntegerList next; public IntegerList (int x, IntegerList rest) { data = x; next = rest; } // next slide

  12. List structure public IntegerList (int x) { data = x; next = null; } public int GetHead () { return data; } // next slide

  13. List structure public IntegerList GetBody () { return next; } public int Length () { if (next==null) return 1; return (1 + GetBody().Length()); } public int GetTail () { if (next==null) return data; return GetBody().GetTail(); }

  14. List structure public IntegerList Search (int x) { if (data==x) return this; if (next==null) return null; return GetBody().Search(x); } public int ExtractElement (int index) { // This is slower compared to array if (index==0) return data; if (next==null) { System.out.println (“Query index too large!”); return -1; } return GetBody().ExtractElement (index-1); }

  15. List structure public void Enqueue (int x) { if (next==null) { next = new IntegerList (x); } else { GetBody().Enqueue(x); } } // next slide

  16. List structure public IntegerList Dequeue () { return next; } public IntegerList Reverse () { if (next==null) return this; IntegerList temp = GetBody().Reverse(); temp.Enqueue (data); return temp; } // next slide

  17. List structure public void SetBody (IntegerList x) { next = x; }

  18. List structure public void Print () { if (next==null) { System.out.println (data); } else { System.out.print (data + “, ”); GetBody().Print(); } } // next slide

  19. List structure public IntegerList SearchAndDelete (int x) { // return the new head of the list IntegerList temp = this; IntegerList prev = null; while ((temp != null) && (temp.GetHead() != x)) { prev = temp; temp = temp.GetBody(); } // next slide

  20. List structure if (temp != null) { // found x if (prev == null) { // first one is x return temp.GetBody(); // new head of list } else { prev.SetBody (temp.GetBody()); return this; } } else { // did not find x return this; } } // next slide

  21. List structure public IntegerList SortedInsert (int x) { // return the new head of the list IntegerList temp = this; IntegerList prev = null; IntegerList newBorn = new IntegerList (x); while ((temp != null) && (temp.GetHead() < x)) { prev = temp; temp = temp.GetBody(); } // next slide

  22. List structure if (temp != null) { if (prev == null) { // Insert at head newBorn.SetBody(this); return newBorn; } else { // Insert in middle prev.SetBody(newBorn); newBorn.SetBody(temp); return this; } } // next slide

  23. List structure • else { // Insert at end • prev.SetBody(newBorn); • return this; • } • } • } // end class • Sometimes a tail reference helps • Could enqueue at end without traversing the entire list

  24. List structure class ListBuilder { public static void main (String a[]) { IntegerList iL = new IntegerList (5); iL = new IntegerList (6, iL); iL = new IntegerList (-2, iL); iL.Enqueue (13); System.out.println (“Length: ” + iL.Length()); System.out.println (“Position 2: ” + iL.ExtractElement (2)); iL.Print (); // next slide

  25. List structure iL = iL.Reverse(); iL.Print(); iL = iL.SearchAndDelete(-2); iL.Print(); iL = iL.SortedInsert(10); iL.Print(); } }

  26. Maintaining a tail reference • Want to have a “global” tail reference shared by all the objects in the list • Need to declare tail as a static variable • These are also called class variables because these are attached to a class as opposed to specific objects

  27. Maintaining a tail reference public class IntegerListWithTail { private int data; private IntegerListWithTail next; private static IntegerListWithTail tail = null; // Could maintain a global length also private static int length = 0; public IntegerListWithTail (int x, IntegerListWithTail rest) { data = x; next = rest; if (tail == null) tail = this; length++; } // Next slide

  28. Maintaining a tail reference public IntegerListWithTail (int x) { data = x; next = null; tail = this; length++; } public static int GetTail () { if (tail == null) return -1; else return tail.GetHead(); } // Next slide

  29. Maintaining a tail reference public static int Length() { return length; } public IntegerListWithTail SearchAndDelete (int x) { // return the new head of the list IntegerListWithTail temp = this; IntegerListWithTail prev = null; while ((temp != null) && (temp.GetHead() != x)) { prev = temp; temp = temp.GetBody(); } // Next slide

  30. Maintaining a tail reference if (temp != null) { // found x length--; if (prev == null) { // first one is x return temp.GetBody(); // new head of list } else { prev.SetBody (temp.GetBody()); // update tail if (temp.GetBody() == null) tail = prev; return this; } } // Next slide

  31. Maintaining a tail reference else { // did not find x return this; } } public int GetHead () { return data; } public IntegerListWithTail GetBody() { return next; } // Next slide

  32. Maintaining a tail reference public void SetBody (IntegerListWithTail x) { next = x; } } // end class class ListBuilder { public static void main (String a[]) { IntegerListWithTail iL = new IntegerListWithTail (5); iL = new IntegerListWithTail (6, iL); iL = new IntegerListWithTail (-2, iL); System.out.println (“Length: ” + IntegerListWithTail.Length()); // Next slide

  33. Maintaining a tail reference iL = iL.SearchAndDelete(-2); System.out.println(“Tail: ” + IntegerListWithTail.GetTail()); iL = iL.SearchAndDelete(5); System.out.println(“Tail: ” + IntegerListWithTail.GetTail()); System.out.println(“Length: ” + IntegerListWithTail.GetLength()); } }