Constructors It can be tedious to initialize all
Constructors
Ø It can be tedious to initialize all of the variables in a class each time an instance is created. Ø Java allows objects to initialize themselves when they are created. Ø This automatic initialization is performed through the use of constructor. Ø A constructor initializes an object immediately upon creation. Ø It has the same name as that of class. Ø Once defined, the constructor is automatically called immediately the object is created, before the new operator completes. Ø Constructors do not have return type, not even void. Ø This is because, the implicit return type of a class constructor is the class type itself. Ø It is the constructors job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.
Ø Allocating an object, syntax class_variable = new class_name(); Ø Now you can understand why the parentheses are needed after the class name. Ø What is actually happening is that the constructor for the class is being called. Ex: Box mybox = new Box(); Ø new Box() is calling the Box() constructor. Ø When you do not explicitly define a constructor for a class, then Java creates a default constructor for a class. Ø The default constructor automatically initializes all instances to zero. Ø Once you define your own constructor, the default constructor is no longer used.
Parameterized Constructors Ø It is not very useful to initialize all objects with same values. Ø What is needed is a way to construct objects of various dimensions. Ø The easy solution is to add parameters to the constructor.
The this Keyword Ø this can be used inside any method to refer to the current object. Ø That is, this is always a reference to the object on which the method was invoked. Ex: Box(double w, double h, double d) { this. width = w; this. height = h; this. depth = d; } Ø The use of this is redundant, but perfectly correct. Ø Inside Box(), this will always refer to the invoking object.
Instance Variable Handling Ø The instance variables of a class and the formal parameters to methods can have same names. Ø When a local variable has the same name as an instance variable, the local variable hides the instance variable. Ø While it is usually easier to simply use different names, this can be used instead. Ø Because this lets you refer directly to the object, you can use it to resolve any name space collisions that might occur between instance variables and local variables. Box(double width, double height, double depth) { this. width = width; this. height = height; this. depth = depth; }
Garbage Collection Ø In Java destruction of object from memory is done automatically by the JVM. Ø When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object are released. Ø This technique is called Garbage Collection. Ø This is accomplished by the JVM. Ø Unlike C++ there is no explicit need to destroy object. Advantages of Garbage Collection 1. Programmer doesn't need to worry about dereferencing an object. 2. It is done automatically by JVM. 3. Increases memory efficiency and decreases the chances for memory leak.
What is the meaning of no reference to an object (unreferred object)? Ø By nulling the reference Ex: Box b 1 = new Box(); b 1 = null; Ø By assigning reference to another object Box b 1 = new Box(); Box b 2 = new Box(); b 1 = b 2; Ø By anonymous object new Box();
The finalize() Method Ø Sometime an object will need to perform some specific task before it is destroyed such as closing an open connection or releasing any resources held. Ø To handle such situation finalize() method is used. Ø finalize() method is called by garbage collection before collecting object. Ø Its the last chance for any object to perform clean-up utility. Ø Inside finalize() method you will specify those actions that must be performed before an object is destroyed. Ø The garbage collector runs periodically, checking for objects that are no longer referenced. Ø Right before an asset is freed, Java run time calls the finalize() method.
Syntax: protected void finalize() { //finalize-code } Protected, because that prevents access to finalize() by code defined outside its class.
Creating array of Objects Student[] s 1 = new Student[3]; //array of Student objects Student[0] = new Student(); Student[1] = new Student(); Student[2] = new Student(); for(int i=0; i<3; i++) or Student[i] = new Student();
Difference between Buffered. Reader and Scanner Buffered. Reader Reads only String Present in Java from JDK 1. 1 version Less powerful because can read only string Scanner Reads both String and other data types like int, float, long, double, float etc Scanner is newer, introduced in Java 5 More powerful because it can parse the user input and read int, float, short, long, double, etc Has large Buffer size (8 KB) Has less size (1 KB) Helpful when reading long String from file Helpful for short input and input other than String Buffered. Reader is synchorinized, i. e. , can Scanner is not synchorinized. i. e. , cannot share Buffered. Reader between multiple share scanner between multiple threads
Difference between Buffered. Reader and Scanner contd… Buffered. Reader Requires: import java. io. Buffered. Reader; import java. io. Input. Stream. Reader; Scanner Requires: import java. util. Scanner; Throws exception Does not throw exception Useful when you want to read input without Useful when input is not big and also to read parsing. As it has larger buffer, can also be different types of inputs used to read long String
A Stack Class Test. Stack. java
References Ø Herbert Schildt, “The Complete Reference, JAVA”, 7 th ed
Thank You
- Slides: 16