Anatomy of a class

Earlier we discussed about how java program executes. Here we will learn what happens when we run a java program. When the JVM starts running it –

  • It looks for the class we give it at the command line.
  • Then it starts looking for a specially-written method that looks exactly like:

             public static void main(String[ ] args)  {

                  //code goes here

             }

  • Next, the JVM runs everything between the curly braces { } of our main method.
  • Every Java application must have at least one class, and at least one main method (not one main for every class but one main per application).

public class Mobile {

      public static void main(String[ ] args) {

            System.out.println(I will print anything that is in this method) ;
       }

}

In the above code, running the program means telling JVM to “load the Mobile class, then execute its main ( ) method and keep running till all the code in the main is finished”.

So, what is a class?

  • A Class is a plan/structure/blueprint of an Object (further we will learn more on objects).
  • It defines or describes an Object i.e. it tells the virtual machine how to make an object of that type.
  • Example: you might use the Mobile class to make dozens of different mobiles, and each mobiles may have its own type of brand, color, price, size and so on.
  • It is a collection of similar types of Objects.
  • Syntax:

class ClassName 

          State;

          behaviour:

}

Name conventions

There are certain rules to name a class. It’s not compulsory but it is a good practice to follow these conventions as it helps us to identify and differentiate class and objects in complex code. All companies follow these rules foe future reference.

  1. Class of one word:

          First letter of the class name should be in UPPER-CASE

E.g.     class Demo {

        }

  1. Class of more than one word:

          Second word or so on’s including first letter should be in UPPER-CASE.

E.g.      class CarTestDrive {

}

I would prefer you to open any code in your books, notebooks and observe the class names and relate it with what we have learnt just now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s