In early binding, the decision about which method code and field to use is made at compile time.
Early binding is used for the following types of methods and fields of a class in Java:
class MySuper { // An instance variable public String str = "MySuper"; // A static variable public static int count = 100; public static void print() { System.out.println("Inside MySuper.print()"); }/*from w ww.j a va 2s .c om*/ } class MySub extends MySuper { // An instance variable public String str = "MySub"; // A static variable public static int count = 200; public static void print() { System.out.println("Inside MySub.print()"); } } public class Main { public static void main(String[] args) { MySuper ebSuper = new MySuper(); MySub ebSub = new MySub(); // Will access MySuper.str System.out.println(ebSuper.str); // Will access MySuper.count System.out.println(ebSuper.count); // Will access MySuper.print() ebSuper.print(); // Will access MySub.str System.out.println(ebSub.str); // Will access MySub.count System.out.println(ebSub.count); // Will access MySub.print() ebSub.print(); // Will access MySuper.str System.out.println(((MySuper) ebSub).str); // Will access MySuper.count System.out.println(((MySuper) ebSub).count); // Will access MySuper.print() ((MySuper) ebSub).print(); // Assign the ebSub to ebSuper ebSuper = ebSub; // Upcasting /* * Now access methods and fields using ebSuper variable, which is referring * to a MySub object */ // Will access MySuper.str System.out.println(ebSuper.str); // Will access MySuper.count System.out.println(ebSuper.count); // Will access MySuper.print() ebSuper.print(); } }