ClASS AND METHODS

java class and method

A object is any real time entity.e.g Human, Car, Bus, Pen etc. Every Object will have State & Behaviour.Sate will be stored in Field(Variables) and behavior will be take care by method.
A Class is blueprint of the Object, based on the class individual  objects are created.
 
eg: Human Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Human
String Color;
float weight;
double height;
   void sleeping()
     {
     
    }
   void eating()
     {
      
      }
    void running()
     {
     }
    }
 
There are three kind of Variable available in java
Local Variables: Variables declared & initialized inside method, block or constructor is called local variable. Scope of these kind of variable is up to that method/block/constructor. Local variables will be destroyed once method,block or constructor will be executed. Local variables cannot be accessed outside of its method,block or constructor.
Instance Variables: Variables declared outside of method, constructor & block but inside the class is called as Instance Varibales.Instance variable can be accessed from within any method, block or constructor.
Static Variables: Variables declared inside Class with Static keyword is called as Static variable. Static Variables are called as Class variables also. Static Varibales can be access by ClassName.VariableName.
 
Creating Object
Declaration, Instantiation and Initialization are the three main steps involved in creating the object.
 
1
2
3
4
Public Class Human
  {
  Human h1=new Human();
  }

In above example left hand side called as Reference type and right hand side as Object type.h1 is of Human class type and it referring to the object of Human Class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Public class Human
   {
    
   }
     class Indian extends Human
  {
      Human h1=new Human();
      Indian i1=new Indian();
      /*
      * Super Class can be reference type for sub class object
      * But Sub Class cannot be reference for super class object.
       * */
       Human h2=new Indian();
       Indian i2= new Human();// This line will give error.
 }