Fallowing icon

Friday, September 28, 2012

Java Class and Methods



Class

         Every java program must have one or more classes. We create object,variables, method etc in class body. We can say ether class is the template of the object or class is the blueprint from the witch individual objects are created.All object with the same characteristics and behavior belong to the same class. 



Example for class-



The class name is Car, There are two methods call driving and parking in this class.




Methods
        


        public static void main(String args[]){}

       I think this command is familiar to you, This is the main method in java program. public and static are the method modifiers and void is method return type stat 

public, static - You can learn this in advance java lessons. 
void   - There are no values return in this class.
main - main is the class name, every class must have a class name and it want to be a unic word.


Discussion-


      
You can see what are the Methods and what are the Attributes in School class. 
              


                              void getStart(int temp, double voltage, double oilLeval){
                                        //process the start 
                              }  


      public is the method modifier and void is the return type. Then you can see round brackets, we call it parameter. We declare the three variables in parameter, temp, voltage and oilLeval. Those variables are in method block. This inverted commas are used for create method body. 


How to call methods


           Before we call methods, we want to create class object which object is situated. then you can use this object variable and can access the methods. 

Example-



             public class Test { 
        
                          void add(){                                                 //declare the methods
                         double x=5,y=3;
                         System.out.println("x + y is ="+x+y);
                          }
                          void sub(){
                         double x=5,y=3;
                         System.out.println("x - y is ="+(x-y));
                          }
                          void mult(){
                         double x=5,y=3;
                         System.out.println("x * y is ="+x*y);
                          }
                          void divi(){
                          double x=5,y=3;
                          System.out.println("x / y is ="+x/y);
                          }
    
                          public static void main(String[] args) {
                                 Test a=new Test();                                     //declare the object
                                 a.add();                                                       //call the methods
                                 a.sub();
                                 a.mult();
                                 a.divi();        
          
                        }
               }








  
Test a=new Test();  - You can learn next lesson.


        a.add();
        a.sub();
        a.mult();
        a.divi();

When you type a.add(); , It's like a path. Now you in add method and execute it. You can call any methods using this. 



Constructors 

        Constructors are the most important for us, Every classes have got constructors. Which we are not created the constructors in class, But java compiler builds default constructors for it.

Structure of constructor-


        class Apple{
             
                           public Apple(){
                                   //this constructor haven't any parameter. 
                           }
                          Apple(int amount){
                                   //amount is the parameter of this constructor 
                           }
                }


You can see int this structure, constructor name is equal to class the name, It's the rule. You can create one or more constructors in one class. 


Example-


                   public class Test { 

                               int x,y,result;              //declare the variables

                               void Test(){                                                 //declare the constructor 
                               result=x*y;
                               System.out.println("The Land Area is ="+result);
                                }
                                void Test(int value){                                 //declare the constructor and define the value variable in parameter 
                                result=x*y*value;
                                System.out.println("The Land Value is ="+result);
                                 }
    
                                 public static void main(String[] args) {
                                            Test a=new Test();                       //create the object
                                            a.x=30;                                          //call x variable and assign 30 
                                            a.y=50;                                          //call y variable and assign 50
                                            a.Test();                                        //call Test constructor
                                            a.Test(133);                                  //call Test constructor and pass value 133
                                  }
                      }




  




First we create Test constructor and then create Test constructor and give variable for parameter. 

Then create object and assign values for x and y. already you call methods and Now you call variables, Those are same. 
Then call first constructor and get area of this land and secondly call constructor is passing the vale in using parameter. Finally get land value.


    

No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment