Sadegh Aliakbary Sharif University of Technology Fall 2010
Agenda Review of last chapter Access specifiers Static fields and methods Initialization Cleanup Fall 2010Sharif University of Technology2
Review (Arrays) Fall 2010Sharif University of Technology3
Review(1) Fall 2010Sharif University of Technology4
Review(2) Fall 2010Sharif University of Technology5
Review(3) There is three type of variable in this code array reference array[i] references array[i] objects Fall 2010Sharif University of Technology6
Object Scopes Fall 2010Sharif University of Technology7
Access Specifiers public Interface access private Don’t touch! Fall 2010Sharif University of Technology8
Example Fall 2010Sharif University of Technology9
Public classes You can declare a class which is not public The class is visible within the file There can be only one public class per file The name of the public class must exactly match the name of the file including capitalization It is possible to have a java file with no public class Is not typical Fall 2010Sharif University of Technology10
Static properties static properties are shared among all the objects static properties are properties of classes Not objects What is the application? Integer.MAX_VALUE Player.NumberOfObjects Fall 2010Sharif University of Technology11
Static methods Static methods can access only static properties Static methods are actually class operations Not object operations If a method uses only static fields, make it static! Static methods are accessible via classes double d = Double.parseDouble("12"); String s = String.valueOf(12); public static void main(String[] args) Fall 2010Sharif University of Technology12
Constructors Constructor is a special method With same name as the class Without any return type is called when an object is instantiated May have parameters Default constructor : no parameter Is implicitly implemented You can write your own constructor If you write any constructor, default implicit constructor is vanished. Fall 2010Sharif University of Technology13
Constructor example Fall 2010Sharif University of Technology14
Fall 2010Sharif University of Technology15
Fall 2010Sharif University of Technology16
Method Overloading Methods with the same name in the same class With different parameters Fall 2010Sharif University of Technology17
Fall 2010Sharif University of Technology18
Return type method overloading Why this is not permitted? Fall 2010Sharif University of Technology19
Method-property access How does getArea() know where radius is? Fall 2010Sharif University of Technology20
Compiler solution A reference to the object is implicitly passed to methods circle.getArea() is converted something like: Circle.getArea(circle) What if you want to access this circle object? Use this keyword this is available within non-static methods Fall 2010Sharif University of Technology21
Sample Fall 2010Sharif University of Technology22
Static again What are static methods? Methods without implicit this Static methods are bound to classes, not objects Fall 2010Sharif University of Technology23
Finalize method Java has no delete Java has no destructor Java has a special method: finalize finilize() is called when the object is garbage- collected Why we may need finalize? Garbage collection is only about memory Fall 2010Sharif University of Technology24
Quiz! Write a java class for representing … Fall 2010Sharif University of Technology25
Fall 2010Sharif University of Technology26
Fall 2010Sharif University of Technology27