Hi,
can any one exain me the concept of static and dynamic loading in core java??
/* program for to display the product details by using jdbc. For example take the product table with some columns like product pid pname price database : Oracle username : chavan password: chavan */ import java.sql.DriverManager; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class ProductDetails { static Connection con; static PreparedStatement ps; static ResultSet rs; public static void main(String args[]) { try{ Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "chavan","chavan"); ps=con.prepareStatement("select * from product"); rs=ps.executeQuery(); while(rs.next()) { System.out.println("pid="+rs.getInt("pid")+" pname="+ rs.getString("pname")+" price="+rs.getDouble("price")); } } catch(ClassNotFoundException cnfException) { cnfException.printStackTrace(); } catch(SQLException sqlException) { sqlException.printStacKTrace(); } finally { try{ if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(con!=null) con.close(); } catch(SQLException sqlException) { sqlException.printStackTrace(); } } } }//end to class Note : before run the program set the class path to ojdbc14.jar file
Hi Akash,
static loading done by compiler at compile time. But dynamic loading done by jvm at run time. Method overloading and method overhiding are examples for static loading. Method overriding is example for dynamic loading.
overloading:
ex: method1(int a,float b) {} method1(float b,int a) {}
In this example the method name is same and both take two parameters one is int and anoter one is float. But hear the oreder is different.
ex: class Sample { void method1(byte... b) { System.out.println(" byte variable argument"); }
public static void main(String[] args) { Sample s=new Sample(); byte a=2; s.method1(a); // observe this line carefully } } o/p : byte variable argument program description: The static loading(overloading &over hiding) done by the compiler "" based on reference variable"". s.method1(a). In this the complier perform the static loading based on "s". this s variable belong to Sample class type. In this the compiler check any metod1 is there or not in the Sample class . If it is there, it check the argument of the method1 is it byte or not. If it is byte the compiler decides for execution.But if it is not their it perform the "winding operation ". " it conver the byte into short and it check for method1(short s). If it is their it decide that method for execution. If it aslo not there it conver the short into int
and it check for method1(int) and repeat the operation unitl
byte->short->int-> long ->float-> double.
method1(double). If this method also not there in Sample it perform the "Boxing "
In boxing the compiler convert the byte vale which is passed in method1(byte) into Byte class object and search a method1(Byte). If it is their it decide the method1(Byte) for execution.If it is not their it check the method1 which take the argument as Byte super class like method1(Object) .If it is their it decide the method1(Object) for execution. If it is also not their the compiler search a method which take as variable argumet line
method1(byte... b) or method1(Byte... b) or method1(short...)or .... method1(double... ). If it is their the compiler decide the method for execution. This method also not theri it show compile thime error like "can not find symble" .
rules: according to the overloading 1.the compiler expect only one metho which is matched with given arguments. 2.the compiler give the first preority for winding and it give nexe preority for boxing. 3.the compiler give the last preority for variable argument. 4. the compiler perform the static loading based on reverence variable( example s). 5. the overloading for instance and static methods but not a constructors.
over hiding:
ex: class Parent { public static void method1() { System.out.pritnln("Parent static method"); } }
class Child extends Parent { public static void method1() { System.out.println("Child static method"); } public static void main(String arg[]) { Parent p=new Parent(); p.method1(); // Parent static method Child c=new Child(); c.method1(); // child static method Parent p1=new Child(); p1.method1(); // Parent static method } } This method overhinding performed by the compiler based on the reference variable. In p.method1() we are using p which is belong to Parent type hence the compiler decide the method1() in Parent type. In c.method1() we are using c which is belong to Child type hence the compiler decide the method1() in child class. In p1.method1() hear also p1 is type of Parent hence the compiler decide the method1() in Parent class for execution. To looking this is same like method overriding but it is not overriding. Method overriding only for instance method not for static methods. But over hiding only for static methods. In the method1() in Parent and Chid both are different.
rules: 1. it is only for static members 2. it is performed by the compiler at compile time based on refernce variable. 3. it is not for constructors and instance methods.
overriding:
overriding is performed by the jvm at runtime based on object reference (address or hashcode). " a method in super and sub classes contain same method signature( it mean method and parameters) and same return type or parent method covarient return type (for example if parent method return type is Object then it child method return type is Object or Object subclasses)."
ex: class Parent { Object method1(Integer a) { System.out.println("in parent a="+a); return a; } }
class Child extends Parent { public Integer method1(Integer a) { // covarient return type System.out.println("in Child a="+a); return a; } public static void main(String args[]) { Parent p=new Child(); p.method1(5); } } p.method1(Integer). In compile the the compiler take the p Which is Parent type. Hence it check any method1(Integer) their or not. In Parent it is their hence their is no problem
But in run time the jvm the reference(hashcode) of new Chile(). and at first it check is there any method1(Integer)in Child ,if it is their the jvm execute that method. In Child there is no any method1(Integer) the jvm search the method1(Integer) in Parent. If it is there it execute the method1(Integer). Hence method overriding performed by the jvm based on the reference of the object.
rules : 1. jvm perform the overriding based on reference of the object 2. jvm search any method at first in child and next in it's parent. 3. according to the overriding the the child class method return type is same as parent class method return type or it sub classes. 4. it is not possible to provide the weeker access specier for child class method
in the above example the paent class method contain default access specier. Hence the sub class method access specier may be deafult or it higher access specier.
private < deafult < protected < public
5. it is not posiible to incress the size or level of checked exceptions to the child class method. ex: parent : void method1() throws IOExcepton size=1 child : void method1() throws IOExcepton size=1 parent : void method1() throws IOExcepton size=1 child : void method1() throws IOExcepton,FileNotFoundException size=2 hence compile time error parent : void method1() throws IOExcepton child : void method1() throws Excepton compile time error because hear we are increse level it mean Exception is super class of IOException 6. it is not possible to override the private, static ,final mehods and constructors. 7.the method in super class is called overriden method and the method in supclass is called overriding method. 8. it is done by the jvm at runtime based on reference.
Ads