Monday, 19 October 2015

WHAT IS A CLASS AND WHAT IS AN OBJECT IN JAVA..??

INTRODUCTION:

     Hey..folks ..!! In this post we are going to look about the classes and objects in java.


What is an object???


   An object is a combination of properties and actions. 

   Example 
       Consider the image below is "You " . We have to take two things into consideration.
                                          
 What do you have ? and
  What do you do ?

  • You have brown hair
  • you wear pink dress etc  and

     You will eat 
     sleep
     walk etc

So an object is any real world thing that you want to present in java. So, I can present your information in java as ,


Properties :

Hair_Colour = brown and
Dress_colour =pink

and I can represent the actions you do as


Actions:

eat()
sleep()
walk()

So, an object is a combination of properties and actions.




What is a  CLASS:

        A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.
       Java class objects exhibit the properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class.
      A class is a package of variables and method definitions.If you want to know what a variable is refer my post which has about variable .

Creating a class in java:

       So..by reading till this ..ufff..enough of all these boring stuffs.. is what you will think.. :D !! Don't worry lets jump to programming and make it interesting:)

All the java class creation will start with,


Class classname

{


}


But ..omg we cant make anything interesting without a boss who is nothing but a main() method.Here he comes..!! 

After his presence the syntax now changes to..

Class classname
  {
  public static void main(String args[])
        {

         }

}

This is what the syntax is.. !! We can do many inside this gigantic extraordinary main() method.. !!!

But yea.. next question obviously will be what the rest of the keywords "public static void" doing ??


            public static void main() 


public => the main method is public  ,meaning the method main() is not limited to that particular package or particular class. The method can be accessible from anywhere.
 (If you don't know what the package is, I will cover that in upcoming posts)

Why is main ( ) method static:


    static => static is a keyword meaning only one copy exists and while invoking a static method there is no need for object creation.

     This is the reason why the main method is static. 
 You would have noticed we haven't created any object initially..! Have we?...
     nope..let me recollect you with an example..

Class classname

  {
  public static void main(String args[])
        {
           System.out.println("hai dude..!!");         }
}

On executing the above program the output will be hai dude..!!

So..wow.. we didn't create any objects here. !!

Technical story:


         If a java application has to be executed,there has to be some starting point . Our boss main( ) is the starting point but, java doesn't allow you to execute any method unless you create an object of the class where the method resides. Unless we execute the code how can we create the instance of the class??


       To resolve this,main ( ) has to be static  . As I said earlier if the main method is static there is no need for object creation and the method will get executed even without creating an instance of the class.

    If you don't know what an instance actually mean..please don't worry friends..will explain you in detail later.(Will cover in depth about the static keyword too in upcoming posts.. :) )

Short story :


    main ( ) method : hey compiler im going to execute for the first time .. Help me do so..

     Compiler : hey main ( ) .. you don't have any object creation done..how can i execute you?? Do some object creation because it's is the one which will help you execute.. you understood??

     main ( ) : hey compiler i can't create an object if I don't execute..
 stupid..help me man...

     Compiler : ok .. I will give you an idea.. make yourself as static so that I can execute you directly and no need for object creation..
 how about that :) :D

    main ( ) : I am so glad to have to man.. thanks a lot.. I will make myself static 

  So this is the reason.. why it is static.. hope you understood


Conclusion:

    So, we have now understood the foundations of java which are
class and objects. Please drop your comments below and help me post more. Your feedback is welcomed. For further queries email me and stay tuned for more posts.. !! Have a nice day :)

No comments :

Post a Comment