Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sadegh Aliakbary Sharif University of Technology Fall 2010.

Similar presentations


Presentation on theme: "Sadegh Aliakbary Sharif University of Technology Fall 2010."— Presentation transcript:

1 Sadegh Aliakbary Sharif University of Technology Fall 2010

2 Agenda Review of last chapter Access specifiers Static fields and methods Initialization Cleanup Fall 2010Sharif University of Technology2

3 Review (Arrays) Fall 2010Sharif University of Technology3

4 Review(1) Fall 2010Sharif University of Technology4

5 Review(2) Fall 2010Sharif University of Technology5

6 Review(3) There is three type of variable in this code array reference array[i] references array[i] objects Fall 2010Sharif University of Technology6

7 Object Scopes Fall 2010Sharif University of Technology7

8 Access Specifiers public Interface access private Don’t touch! Fall 2010Sharif University of Technology8

9 Example Fall 2010Sharif University of Technology9

10 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

11 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

12 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

13 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

14 Constructor example Fall 2010Sharif University of Technology14

15 Fall 2010Sharif University of Technology15

16 Fall 2010Sharif University of Technology16

17 Method Overloading Methods with the same name in the same class With different parameters Fall 2010Sharif University of Technology17

18 Fall 2010Sharif University of Technology18

19 Return type method overloading Why this is not permitted? Fall 2010Sharif University of Technology19

20 Method-property access How does getArea() know where radius is? Fall 2010Sharif University of Technology20

21 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

22 Sample Fall 2010Sharif University of Technology22

23 Static again What are static methods? Methods without implicit this Static methods are bound to classes, not objects Fall 2010Sharif University of Technology23

24 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

25 Quiz! Write a java class for representing … Fall 2010Sharif University of Technology25

26 Fall 2010Sharif University of Technology26

27 Fall 2010Sharif University of Technology27


Download ppt "Sadegh Aliakbary Sharif University of Technology Fall 2010."

Similar presentations


Ads by Google