Pages

Friday 7 September 2012


when we write our class in java, it is not full pledged one.
in fact more than 95% of developers wont write full pledged program
so compiler will convert our program into full pledged program.

1)our program
SoftwareEngineer.java

public class SoftwareEngineer {
      String name;
     static double salary;

      public SoftwareEngineer() {
            name = "kb";
             salary=20000;
      }
      void print(){
            System.out.println(name);
            System.out.println(salary);
      }
      void change(String new_name,double new_salary){
            name=new_name;
            salary=new_salary;
      }
     

}

The same  program after compiling becomes like this

2) full pledged program created by compiler
SoftwareEngineer.class

public class SoftwareEngineer extends Object {
      String name;
    static double salary;

      public SoftwareEngineer(SoftwareEngineer this) {
            super(this);
            this.name = "kb";
            SoftwareEngineer.salary=20000;
      }
      void print(SoftwareEngineer this){
            System.out.println(this.name);
            System.out.println(SoftwareEngineer.salary);
      }
      void change(SoftwareEngineer this,String name,double salary){
            this.name=name;
            SoftwareEngineer.salary=salary;
      }
     

}

What all the things compiler added to make our program into full pledged one
1)making Object class as a super class to our class.
Every class in JAVA has Object class as the super class.

2)constructor and other methods got one extra argument 
Yes every method will get one extra argument of type current class which is named as "this".

3)constructor got one extra statement
Yes Every constructor must have the first statement as either super() or this() with or without the arguments.
if we dont write any one of those as the first statement inside constructor , compiler will automatically add super(this) as the first statement inside the constructor.

so try to explain what is "this" and where and all its added. and what is its significance in the program.

also try to figure out why there is no this.salary  in place of SoftwareEngineer.salary   inside a constructor ?

No comments:

Post a Comment