Monday 13 February 2012

Tips Need To be remembered for OCJP Exam :

 1.> There should only one public class per source file.

 2.> There can be any number of default class in a single source file 

 3.> Class can be declared only as default or public because if it declared as private it will not be available to JVM. 

4.> If there are two are more class are present in a single source file then if we compile that source file it will be segregated into several dot class files each with file name and its corresponding source code in form of bytes.

5.> A class which is declared as public in a source file should have same name as the file name.
Why Dot class file name and name of the class should be same?
  
   ANS : When JVM searches for class it will look at the file name of the Dot class file , it will not look in to the byte code of the Dot class file . 

      So if  we are searching for a particular class let us say "House" class in a particular JAR file ,  there is a House class in that  JAR File but the file name[byte code file] of that  House class is Apple then ,JVM cannot able to figure out House class , so inorder to avoid that confusion Dot class file name and class name should be same.

See the below image to understand how it works....


[Any Doubt please  , Feel free to ask i will try my level best to clear your doubt]
State And Behavior Of the object :

   State Of Object : Every Object has an state ,state means things an object holds, let us take ourself Human; A 'Human' object can have[hold] leg, hand,name,color, and so on .. Which are called state.

 Behavior Of the object : The actions which can be done by the object , in our Human Object we can talk,run,swim,play which are all called Behavior of the Object

Important :
 In Simple words , INSTANCE VARIABLES of the class are called STATE of object. And the METHODS are called BEHAVIOR of object.

See the below Diagram to understand the State and Behavior of object.
State And Behavior Of object



What is Class And Object in JAVA ?

 Class :-
   Class is Blue Print [I hope you will not ask me what is mean by blue print.. if you don't know then just Google it..] or you can say it is a template for creating an object.
  
 For EX, Now lets create a "Bird" class , So what are things needed to create a 'Bird' class think.... 

  Finished thinking .... ok lets go to subject

 So for Bird Class we need Color,Name,CanFly,Size and so on... 

Now let us create a blue print for the class Bird :

 public class Bird {
                 String name ;
              String color;
           char size;
}
    So now we have a bird class which is a blue print for any kind of Bird Object  which we create ; means ,when we create a Bird object which will have name,color , size[what ever bird it may be].
   
    So once again ,when ever we create a bird object it will have all the state[name,color,size]. Technically the state[name,color,size] are called as instance variables.
   Now let us create a object of this bird class and see how it works.
   public class CreateBirdObjectAndRun {
       public static void main(String[] args) {
        Bird bird = new Bird(); //Here is the Bird Object
        bird.name ="Pigeon";
        bird.canFly = true;
        bird.color ="White";
       bird.size = 'S'; //Here S - Small ,
       
       System.out.println(bird.name);// Here bird object reference.object state[here name], so without object we cannot do anything with only class
       System.out.println(bird.canFly);Here bird object reference.object state[here canFly]
       System.out.println(bird.color);Here bird object reference.object state[here color]
      System.out.println(bird.size);Here bird object reference.object state[here size]
    }
}
 
   The below Diagram show you What is happening with the Bird bird = new Bird() Object

    




1 - Shows the Complete Details of the line Bird bird = new Bird();

2 - 'Bird' is used for creating the space in JVM[Java Virtual Machine].

3. 'new Bird()' creates a New Bird object in  JVM.

4. The 'bird' is now pointing to the Bird object in the JVM. In simple words bird is the reference which holds the reference[memory address] of the Bird Object in JVM.

     We Can create how many objects we want by using the same procedure because we have the Blue Print[Bird class] From which we can create many number of birds like peacock,ostrich,parrot and so on..

    So class are the Blue print from which any number of  objects can be created and used ...

So We have seen that Class is the blue print For creating objects.So now what is Objects?

object:
    Object is nothing but the Instance[one which really exist of particular type] of Class.

  [In general English  Instance means a specific term and the one which exist really for eg ;  The last instance = Here last which tells last occurrence which is specific and and the one which really exist (Don't confuse instance without 'example' ;, example - it may be reality or non-reality but instance means which exist really)]