Akash Agrawal
core java
2 Answer(s)      9 months ago
Posted in : Java Beginners

Hi,

can any one exain me the concept of static and dynamic loading in core java??

View Answers

October 4, 2012 at 2:25 PM


    /* 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

January 3, 2013 at 11:46 AM


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:


two or more methods contain same method name and different number of parameters or order of parameter or type of parameters.

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:


method over hiding only for static methods, not for instance methods. Whenever we declare the static method in side a class , that static methods only for that class. It it not possible to access those static method by another class like subclasses and any another classes.

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.









Related Pages:
core
core  where an multythread using   Please go through the following link: Java Multithreading
core java
core java  how to display characters stored in array in core java
core java
core java  basic java interview question
core java
core java  i need core java material   Hello Friend, Please visit the following link: Core Java Thanks
CORE JAVA
CORE JAVA  CORE JAVA PPT NEED WITH SOURCE CODE EXPLANATION CAN U ??   Core Java Tutorials
Core Java
Core Java  what is a class
core java
core java  Hi, can any one expain me serialization,Deseralization and exterenalization in core java
core java
core java  Hi, can any one exain me the concept of static and dynamic loading in core java
core java
core java  surch the word in the given file
CORE JAVA
CORE JAVA  What is called Aggregation and Composition
core java
core java  how can we justify java technology is robust
core java
core java  what is the use of iterator(hase next
core java
core java  please give me following output
core java
core java  write a java program to view product details from product table
Core Java
Core Java  How to execute cmd command through java?? Give Code of them
core java
core java  its compulsory to save file name and class name is same in java
CORE JAVA
CORE JAVA  What is Garbage collection in java? What is the role of a developer for garbage collection
core java
core java  In java primitive variables will get its default value automatically after declaration. Then why it is mandatory to initialize a variable before using
Core Java
Core Java  Please write a Java Program to design login form and store the values in file & validate and display the MainForm
Core Java
Core Java  Hi, Can any one please share the code for Binary search in java without using builtin function
core java
core java  Hello sir,What is logic behinde the core java programms,How may programmas are there,for example,sorting of two numbers,grade of the student details,fibonice serice,paldroma,incremting of the program,asscedding
core java
core java  Hello sir,What is logic behinde the core java programms,How may programmas are there,for example,sorting of two numbers,grade of the student details,fibonice serice,paldroma,incremting of the program,asscedding
Core Java
Core Java  What is the significance of static synchronized method? Why do we have the method declared as static synchronized
core java
core java  how to compare every character in one string with every character in other string
core java
core java  what is the max size of array?   You can declare up to maximum of 2147483647
Core Java
Core Java  Write a Program to add given number of days to the current system date and display the same
Core Java
Core Java  have to find the prime numbers which is less than the current prime numbers using loops
core java
core java  can i use native keyword with abstract method ? if yes explain and if no please explain
core java
core java  Hi, Can any one please share a code to print the below: 1 121 12321 1234321
CORE JAVA
CORE JAVA  Tell me some Scenarios why you go for Abstract Class and Interface
Core Java
Core Java  How to load class dynamically in java ?   To load class dynamically you can use Class class method Class.forName("abc.xyz.MyClass"); This method load the given class at run time
Core Java
Core Java  Hi, can any one please tell me the uses of return type,"Super" and "this" calling statement in Java?? why do we required this,super calling statement?? why return type is required
Core Java
Core Java  Hi, Can any one please expain me why derived data types are required in java as we have primitive data types with us
Core Java
Core Java  Hi, Can any one please expain me why derived data types are required in java as we have primitive data types with us
core java
core java  readLine() function is realated to which class?   readLine() is a method of java.io.Console class it is also available in java.io.BufferedReader.
core java
core java  what is difference between specifier and modifier? what is difference between code and data? what is difference between instance and object
core java
core java  public class Check { public static void main(String[] args) { System.out.println(11^2); } } how it is work???? plzz explain
Core Java
Core Java  Hi, Can any one please share a code to print the below: 1 23 456 78910 thanks a lot in advance
Core Java
Core Java   How can i write own compile time and runtime exceptions in java   Hello Friend, Please visit the following links: http://www.roseindia.net/java/exceptions/how-to-throw-exceptions.shtml http
Core Java
Core Java  Is Java supports Multiple Inheritance? Then How ?   Hi Friend, Java does not support multiple inheritance but it can be achieved by using the interface. In Java, Multiple Inheritance can be achieved through
Core java
Core java  How to use hyperlink that is href tag in core java without swing, frames etc. My code is StringBuffer oBodyStringBuffer = new StringBuffer("Message Classification: Restricted.\n\n
Core Java
Core Java  Write a program to read the below data from a file and check whether the period is partial or full month? 03/01/2011 â?? 03/28/2011 03/01/2011 â?? 03/31/2011 02/01/2011 â?? 02/28/2011 02/01/2011 â?? 03/01/2011
core java
core java  Create a class containing the main method and define an integer array x of length n=5 to find and display their mean. Mean=?Xi/n
CORE JAVA
CORE JAVA  Iâ??ve a string like below String xyz =â??nullâ??; I want to compare xyz with space which is in other String ABC. What will be the output
CORE JAVA
CORE JAVA  Static methods can be participated in inheritance? What is static keyword? What is static keyword? How to write code for static factory method? What is mutable and immutable
Core Java
Core Java  Write a program to accept the given date in DD/MM/YYYY format and convert the same based on user selection mentioned below? 1.MM/DD/YYYY format 2.YYYYMMDD format
core java
core java  public static void main(String k[]) Is it possible to pass the Integer type arr instead of String type in main method?(replacing String than Integer) if possible.. how to write code
core java
core java  Create a class containing the main method and define an integer array x of length n=5 to find and display their mean. Mean=?Xi/n
core java
core java  Hi, I am facing problem in adding/inserting an elemnts in array. can any one please sahre the code for the same without using collection(Built in function) Regards": Akash
CORE JAVA
CORE JAVA  Which collection is more feasible to store unique objects? Which collection is better to store objects where insertion order should be preserved? Can we use Enumeration for displaying list objects

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.