Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

14 initialization & cleanup

423 views

Published on

  • Be the first to comment

  • Be the first to like this

14 initialization & cleanup

  1. 1. Initialization & Cleanup<br />DhrubojyotiKayal<br />
  2. 2. Guarantees initialization of every object<br />Essentially a method<br />Called when an object is created, even when no reference exists<br />The name of the constructor is the same as the name of the class <br />Unusual type of method because it has no return value <br />the new expression does return a reference to the newly created object <br />public class Shape {<br /> public Shape() {}<br />}<br />Shape s = new Shape()<br />Constructor<br />
  3. 3. A constructor that takes no arguments is called the default constructor <br />If you provide no constructor the default constructor is provided by default by the compiler.<br />You can definitely have constructors with one or more arguments<br />public class Rectangle{<br /> private int length;<br /> private int breadth;<br /> public Rectangle(int length, int breadth) {<br /> length = length;<br /> breadth = breadth;<br /> }<br />}<br />Default Constructor<br />
  4. 4. Method overloading allow the same method name to be used with different argument types. <br />Constructors are methods and they can be overloaded too.<br />Method overloading<br />
  5. 5. public class Tree { <br />int height; <br /> public Tree() { <br /> print("Planting a seedling"); <br /> height = 0; <br /> } <br /> public Tree(intinitialHeight) { <br /> height = initialHeight; <br /> print("Creating new Tree that is " + <br /> height + " feet tall"); <br /> }<br /> public void info() { <br /> print("Tree is " + height + " feet tall"); <br /> } <br /> public void info(String s) { <br /> print(s + ": Tree is " + height + " feet tall"); <br /> } <br />}<br />Overloading in Action<br />
  6. 6. Test the Tree class by invoking each constructor and each method<br />Exercise<br />
  7. 7. Each overloaded method must take a unique list of argument types <br />Even differences in the ordering of arguments are sufficient to distinguish two methods, although you don’t normally want to take this approach because it produces difficult-to-maintain code <br />Distinguishing overloaded method<br />
  8. 8. public class OverloadingOrder { <br /> static void f(String s, inti) { <br /> print("String: " + s + ", int: " + i); <br /> } <br /> static void f(inti, String s) { <br /> print("int: " + i + ", String: " + s); <br /> } <br /> public static void main(String[] args) { <br /> f("String first", 11); <br /> f(99, "Int first"); <br /> } <br />} <br />Overloading in Action<br />
  9. 9. void f() {} <br />int f() { return 1; }<br />int x = f();<br />f();<br />You cannot use return value types to distinguish<br />Overloading on return value<br />
  10. 10. Reference of the current object that is available to no static methods<br />Used widely in constructors to avoid ambiguity<br />Use if you want to pass a reference of current object to another object.<br />this<br />
  11. 11. There is no destructor in Java<br />Memory is managed automatically by a garbage collector which is part of JVM<br />A JVM may not even have a garbage collector<br />When garbage collector runs is not under programmers control<br />System.gc() – just a hint<br />Common garbage collection technique<br />Mark and Sweep<br />finalization()<br />Called before an object is garbage collected<br />Useless method, should be avoided as you never know when it will be called<br />Use in JNI programs – malloc() , free()<br />Destruction<br />
  12. 12. Q&A<br />

×