Lecture 9


Published on

Notes on core java

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lecture 9

  1. 1. Core Java Debasish Pratihari Method Declaration:   Method declaration has two parts  Method Header  Method Body Method Header : o consists of modifiers (optional), return type, method name, parameter list and a throws clause (optional) o types of modifiers     private public protected final     Abstract Static synchronized native Note :  Static methods can only access static methods and static fields.  Final methods cannot be overridden in sub-class  Abstract methods have no body Method invocations :  invoked as operations on objects/classes using the dot ( . ) operator  static method: o Outside of the class: “reference” can either be the class name or an object reference belonging to the class o Inside the class: “reference” can be omitted  non-static method: o “reference” must be an object reference Method overloading:  A class can have more than one method with the same name as long as they have different parameter list. public class Lakshya { ... public void setPrice (float newPrice) { price = newPrice; } public void setPrice () { price = 0; } } 1Lecture/core/oops2/09 Page #1 feel the Technology…
  2. 2. Core Java Debasish Pratihari main method :  the system locates and runs the main method for a class when you run a program  other methods get execution when called by the main method explicitly or implicitly  must be public, static and void Constructor:            is special member method has the same name of its class doesn’t return any thing (even if void) can only be called with new operator to create object There must be at least one constructor for every class If no explicit constructor is there in class then java language provides one called default constructor The access specifier of the default constructor is same as the class. is never inherited can be parameterized and overloaded can be used to initialized the object if there is only parameterized constructors present in the super class , then it is became mandatory to provide a constructor in the sub class. 2Lecture/core/oops2/09 Page #2 feel the Technology…
  3. 3. Core Java Debasish Pratihari Default Constructor :   Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. The only action taken by the implicit default constructor is to call the super class constructor using the super() call. class Lakshya{ Lakshya(){ super(); } } Default Constructor public class Lakshya{ public Lakshya(){ super(); } } Java overloaded constructor : Like java method java constructor can be over loaded. class Lakshya{ Lakshya(){ …… } Constructor 1 Lakshya (String name){ …… } } Note :  If a class is having more than one constructor it gives the flexibility to the programmer to instantiate the class in different fashion. Constructor 2 Constructor Chaining :  Every constructor calls its super class constructor. An implied super() is therefore included in each constructor which does not include either the this() function or an explicit super() call as its first statement. The super() statement invokes a constructor of the super class.  The implicit super() can be replaced by an explicit super(). The super statement must be the first statement of the constructor.  It is possible to use this() construct, to implement local chaining of constructors in a class. The this() call in a constructor invokes the an other constructor with the corresponding parameter list within the same class. 3Lecture/core/oops2/09 Page #3 feel the Technology…
  4. 4. Core Java Debasish Pratihari Example : public class Lakshya{ Lakshya(){ System.out.println("Hello"); } Lakshya(int i){ this(); System.out.println("i="+i); } } Arbitrary Number of Arguments : 25%  Note :  Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed. a construct called varargs to pass an arbitrary number of values to a method Example: public class Lakshya{ Lakshya(int...i){ for(int c=0; c<i.length;c++) System.out.println(i[c]); } public static void main(String args[]){ int nums[]={1,2,3,4,5}; Lakshya ob1= new Lakshya(10,20); Lakshya ob2= new Lakshya(10,20,30,40); Lakshya ob3= new Lakshya(nums);  Varargs are treated same as arrays. } } Accessing shadowed Arguments :  A parameter can have the same name as one of the class's fields. If this is the case, the parameter is said to shadow the field.  “this” keyword is used to access the shadowed fields : public class Lakshya{ int a,b Lakshya(int a, int b){ this.a=a; this.b=b; } } Note :  Within an instance method or a constructor, this is a reference to the current object. It is not available to static methods. 4Lecture/core/oops2/09 Page #4 feel the Technology…
  5. 5. Core Java Debasish Pratihari Initializing Fields :  Simple initialization can be done inline. class Lakshya { int a=10,b=20; …. }  If initialization involves some kind of logic then constructors are useful.  The facility given by constructors for instance variables can be achieved through static initializers for class variables. Static Initializers:  A static initialization block is a normal block of 25% code enclosed with curly braces and preceded by the static keyword. class Lakshya{ …. satic{ // initialization code here } }  A class can have multiple static block.  Static blocks are executed exactly in the order as they present in the source code. Initializer Blocks :     Java provides an initializer block as an alternative to constructor . Initializer blocks for instance variable are like static initializer blocks, but without the static keyword. The initializer block is copied into all the constructors without a call to this(). The block can be useful to avoid code redundancy from constructors. 5Lecture/core/oops2/09 Page #5 feel the Technology…
  6. 6. Core Java Debasish Pratihari Java Objects:      Java object can be created by using the new keyword. They are stored in JVM’s heap area. Java objects are nameless. They can be accessed through their reference only. They are garbage collected if they don’t have any reference. Object & Object Reference 25% Lakshya ob = new Lakshya( ); Note: Heap Area  There is another way to create  objects using 100 ob newInstance() method of Class “Class”. Object reference 100 (Object of Lakshya class) Garbage Collection :     It the process of automatically destroying objects that are no-longer referenced by the program. It is a process for automatic memory management. Basic principles of garbage Collection: o Locate objects that can’t be accessed in future. o Re-claim the resources used by those objects. In java an object may have a finalizer: a method that the garbage collector must run on the object prior to freeing the object. Note :  The Finalizer method in java in finalize().  Finalizers are useful to avoid Resource Leak. 6Lecture/core/oops2/09 Page #6 feel the Technology…