390 likes | 671 Views
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.
E N D
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
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; } }
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()); }
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)
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)
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 }
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
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)
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
List structure public class IntegerList { private int data; private IntegerList next; public IntegerList (int x, IntegerList rest) { data = x; next = rest; } // next slide
List structure public IntegerList (int x) { data = x; next = null; } public int GetHead () { return data; } // next slide
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(); }
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); }
List structure public void Enqueue (int x) { if (next==null) { next = new IntegerList (x); } else { GetBody().Enqueue(x); } } // next slide
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
List structure public void SetBody (IntegerList x) { next = x; }
List structure public void Print () { if (next==null) { System.out.println (data); } else { System.out.print (data + “, ”); GetBody().Print(); } } // next slide
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
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
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
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
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
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
List structure iL = iL.Reverse(); iL.Print(); iL = iL.SearchAndDelete(-2); iL.Print(); iL = iL.SortedInsert(10); iL.Print(); } }
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
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
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
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
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
Maintaining a tail reference else { // did not find x return this; } } public int GetHead () { return data; } public IntegerListWithTail GetBody() { return next; } // Next slide
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
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()); } }