Friday 25 October 2013

Session 7:- Package In java.

Introduction about packages:-

  •  Package is nothing but it is folder.
  •  In java we use packages for better Maintains of the application, project search easily.
  • Every package can have direct classes or it can have again package (sub packages).
  • To access any member (instance variable, instance method etc) outside the package but the member should be public.
  • Within package we no need to import class of the package. 
  •  * will import all the class which is directly under the package (not a sub package).  
  • We can’t import more than same java file having the different package. 
  • All 3000+ inbuilt java class. Modularizes many packages.
    • Ex. - java.lang.*;
    •  -java.sql.*;
    •   - java.io.*;
    • - java.util.*;
    • - java.awt.*;
  • All the class which is under java.lang package will be available implicitly and we don’t have to import. 
  •  Package declaration must be first executable statement.  
  • De-referred object which are present in heap will be collected by Garbage Collector and re-uses memory. 
  • When object in which is present in heap can have multiple reference. 
  • But single reference can refer or point to only one object at same time or given time.  
  • When an instance is of primitive Data type that is called as HAS-A Relationship.   
  • When an instance of variable is non-primitive data type that is called as Association Relationship. 


Example 9:- Package

public class Person{
            // instance variable
        int age=25; //HAS-Relationship
String name="JNext Training & Development !!!!!! SUPER"; // Association Relationship
Mobile m; //Association Relationship
// some Method
 }
public class Mobile
{
    double price; //HAS-Relationship
    String company_name; // Association Relationship
    // some method }

Thursday 24 October 2013

Session 6:- Object & Class

         Object:-

  • Object is real world entity which has States & Behavior.
Ex: Pen,Mobile,Person,Book,Car,Tv,College,Computer,fan,Bus,Company..etc
  •  States generally represent property of object.
  •  Behavior represents an action or functionality.
  •  When we Create an object, we required
    • Plan or master copy, it's called Class.
    •  Plan or Class contains states and behavior which will share with another object.
  • Any number of objects created in class.
  •  The entire object works independently. 
  • When object is modified or destroyed it may not affect in another object.
  •  Consider this women is one object so  It has states and behavior.

    (States)    (Behavior)
    -lags       -playing    
    -cap        -walk        
    -hands     -talk    
    -stick      -stand  
    -eyes       -etc   
    -etc
    • States of class can be called as instance variable.
    • Behavior of class can be called as instance method.
    • In general is called as instance member of class because these members are shared with multiple instances.
    • Sometime the instance variable will be initializing to a value or data and then variable value is same in the all instance.
    • If We don’t initialize instance variable than the instance variable value are different for different object.but when we create the object at that time its take default value for that variable.Default value is depend on the data type if instance variable data type int,byte,short,long than its take 0. for double and flout 0.0.
    • for boolean false
    • for String null
    •  In java the object created in heap memory.
    •  The allocation memory will be taken care by JVM.
    •  JVM is allocation new memory when we use keyword new. 

    Syntax:- Object

    Class-name variable name= new Class-name();
      Ex: Fan f =new Fan ();
      •  Left side key word new Fan(); is the object of the Fan class.
      • f is the reference variable.
      •  Fan is data type of the object.
    •   To access multiple member of an object we required reference variable.
    •   Reference variable data type must be same as object type.
                
                         Tree t = new Tree ();        /*compare these two statement
                          int   i = 20;                       Tree and int is data type t and i variable and  
                                                                    After = sign is value.*/
    Syntax:- Class

                class class-name
                {
                    // instance variable (state)
                    // instance method (behavior)
                }
    NOTE:- Class name Start with capital latter(industry standard).

    Ex: class Student      //
    {
       // instance variable 
       String name;
       int rollno;
     //  instance method
    public void  reading()
       {
            System.out.println("Reading");
        }                                   

    •   Class can also be treated as a custom data type.
    •   Reference variable always categories of non-primitive variable.
    •   Any number of reference variable can be refer a single object.
    Ex:
                       L.H.S              R.H.S
                 Fan     f       =    new        Fan();


    •  new Fan(); is object of class Fan.
    •   new keyword is use for create the object of class and initialize heap memory. 
    •  f is reference variable.(non-primitive variable).
    •   Fan is data type of object. Data type is same as the object name.
    •  When a new object is created that object will be shared with the entire instance variable and these instance variables are initialize to default value.
    •  Default values of an instance variable depend on data type of the variable.
    •  Non-primitive instance variable are initialize to NULL.
    •   Primitive instance variable are initialize to appropriate respective value.
    •   Calling a method by passing a primitive value is called as call by value. 
    •   Calling a method by passing an object reference or reference variable is called call by reference.  
  •  Example  :- Call by value
    public class Calculator{
    public void  addNo(int n1,int n2)
    {    
    int n3;
     n3=n1+n2;
    System.out.println (n3);
    }
                  }
    public class Math{
    public static void main(String args[])
    {
    // 1st  way to run this program
    new Calculator().addNo(20,30);
    /* 2nd  way to call this method
    Calculator c=new Calculator ();
    c.addNo(10, 20);
     
  • Example  :- Call by Reference 

    public class Calculator {
    int a=12;
    nt b=34;
    public void  addNo(Calculator c)
    {
     int n3;
     n3=c.a+c.b;
    System.out.println (n3);
    }               
                  }
    public class Math{
    public static void main(String args[])
    {
    Calculator c=new Calculator ();
    c. addNo(c);

    /* when we use new keyword then object is created in heap memory and memory will be initialize. Here we passing the Calculator class object or reference so it called call by reference */

    }