Saturday, November 22, 2014

Day7 (Java Tutorial)...topis- Constructors, and basic concept of java file.

please continue from here-
http://selenium-makeiteasy.blogspot.in/2014/11/day6-java-tutorialtopis-explanation-abt.html

How to save a java file-

         A java file/program can have number of classes and we can save the file with any of the class name. The compilation will go fine and compiler will create the class file for all the classes in the program.
         Now one can run any of the class. The classes which have main method, only those will be executed successfully, if main method not found then will give error(main method not found).
         This is the reason why we give compilation command as
Javac fileName.java
and run command as
java className


ex- here class A can not be run, only class Demo can be executed.
 
class A{
    int a = 10;
}
public class Demo {
    public static void main(String[] args) {
        System.out.println("program starts");
        System.out.println("Program ends.");
    }
}


Note-
  • For each separate class in a java file/program, separate static pool will be created that is each class will have its own static pool.
  • One can write any number of classes in a java file, with any number of java definition block having i.e. that is every class can have its own main method.
  • If any class is declared as public, then file will have to save with the public class name only. 
  • There can not be more than one public class in a file/program.
===================================================
Constructor
---------------- 
  1. Constructor is special type of method which is executed at the time of instance or object creation.
  2. In java, every class should have a constructor, if a class does not have any constructor then compiler writes a constructor at the time of compilation such constructors are called default constructors.
  3. Default constructor will not have any argument.
  4. A programmer can also develop the constructor inside the class, in such cases programmer has to follow below rules-
    •   Constructor should not have any return type not return value             declared.
    •  Constructor name should be same as class name.
  5. When object is created by 'new' operator, the constructor has to be called by the 'new' operator.
  6. While creating an instance of a class if one have to perform any task at the time of instance creation, declare that particular task inside the constructor body.
  7. Constructor are used to initialize instance member (non-static members) of the class at the time of object creation.
  8. In a single java class, we can develop more than one constructor, the argument list should differ b/n the constructors such constructors are called overloaded constructors. The argument list should differ in either of the below 3 ways-
    • Type of arguments should be different.
    • Number of argument should be different.
    • Position of arg should be different.
  9.  When one have to create an instance of a class with different initialization or different operation at the time of instance creation, then we go for overloaded constructors.
  10. When java is invoking the constructors, constructors are invoked based on the argument list.
Note-
  • There can not be any java program without constructor.
  • instance and object both are same. 
 Syntax to create constructor- (Here methodName will be same as className.)

 methodName(args/no args){
       ............................
       constructor body
       ............................
}

Points to be remember-
  • As soon as object is created, it will call the constructor.
  • If one do not have object then constructor will not be called.
  • If one don't write any constructor then compiler will write the default constructor while compilation.
  • If a programmer write even a single constructor, compiler will not write any constructor.
  • Default constructor has the same access modifier as the class.
ex-
1)
public class Demo {
    Demo1(){
        System.out.println("This is constructor which is called by new operator while object creation");
    }
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo object = new Demo();
        System.out.println("Program ends.");
    }
}


Output-program starts
This is constructor which is called by new operator while object creation
Program ends.

2)
public class Demo2 {
    Demo2(){
        System.out.println("This is constructor which is called by new operator while object creation");
    }
Demo2(int a){
        System.out.println("This is constructor which is called by new operator while object creation in which int arg passed. Value of arg: "+a);

    }
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo object1 = new Demo();
        Demo object2 = new Demo(10);
        System.out.println("Program ends.");
    }
}

Output-program starts
This is constructor which is called by new operator while object creation
This is constructor which is called by new operator while object creation in which int arg passed. Value of arg: 10
Program ends.



No comments:

Post a Comment