A class is a specification  of  how to construct something.  Like a blueprint or a pattern, a Java class has exact specifications.  The specification is the class’ contract.
Class contains  method(s) and instant variables.A method is a member function of a class .It consists of block of statements that returns some value to another method .
A class is a user-defined datatype that consists of set of objects of similar type or A class is just a template which contains the various attributes and functions of an object of the class.

for ex: Consider a class bird. A bird class will have the following attributes:

1. Color

2. Height

3. Weight

4. Habitat

5. nature

It will also have the following functions

1. Flight

2. Soundetc.,

If you ask 10 different persons to think of a bird and describe, each one will describe it differently. Because it is just a general name.

An object is a run-time entity of a class. Say, you create an object parrot from the class bird. Then you know the value for all the parameters for this particular object. If you create another object duck, then this object will also have all the parameters defined for the class bird. But, the values for the parameters would be different from that of the object parrot.

Basic Syntax:

About Java programs, it is very important to keep in mind the following points.

  • Case Sensitivity – Java is case sensitive which means identifier Helloand hello would have different meaning in Java.
  • Class Names – For all class names the first letter should be in Upper Case.

    If several words are used to form a name of the class each inner words first letter should be in Upper Case.

    Example class TestClass

  • Method Names – All method names should start with a Lower Case letter.

    If several words are used to form the name of the method, then each inner word’s first letter should be in Upper Case.

    Example public void myMethodName()

  • Program File Name – Name of the program file should exactly match the class name.

    When saving the file you should save it using the class name (Remember java is case sensitive) and append ‘.java’ to the end of the name. (if the file name and the class name do not match your program will not compile).

    Example : Assume ‘MyFirstJavaProgram’ is the class name. Then the file should be saved as ‘MyFirstJavaProgram.java’

  • public static void main(String[ ] args ) – java program processing starts from the main() method which is a mandatory part of every java program..

    Java Identifiers:

    All java components require names. Names used for classes, variables and methods are called identifiers.

    In java there are several points to remember about identifiers. They are as follows:

    • All identifiers should begin with a letter (A to Z or a to z ), currency character ($) or an underscore (-).
    • After the first character identifiers can have any combination of characters.
    • A key word cannot be used as an identifier.
    • Most importantly identifiers are case sensitive.
    • Examples of legal identifiers:age, $salary, _value, __1_value
    • Examples of illegal identifiers : 123abc, -salary


      Java Modifiers:

      Like other languages it is possible to modify classes, methods etc by using modifiers. There are two categories of modifiers.

      • Access Modifiers : defualt, public , protected, private
      • Non-access Modifiers : final, abstract, strictfp

      We will be looking into more details about modifiers in the next section.

      Java Variables:

      We would see following type of variables in Java:

      • Local Variables
      • Class Variables (Static Variables)
      • Instance Variables (Non static variables)

 

Advertisements