METHOD IN JAVA

Method


  • It is also one of the executable block inside the class.
  • Method should contain the return type and its name can be any valid java identifier that includes class name also.

Syntax for Method :

 
                        
                     class Name
                     {
                        Accesslevel  Returntype  methodName(signature)
                        {
                           ----------------;
                           ----------------;     /* body of the Method. */
                           ----------------;
                           return  returnvalue;  /* return value is specific to return type. */
                                               /* return statement can optional in case of return type is void.*/
                        }
                     }     
                        
  • Access level is optional.
  • Method should end with return statement. Return statement should return atleast one value. It should be specific to method return type.
  • If we don’t want to return any value then we should keep method return type as “void”.
  • If method contain return type as void then that method may or may not end with return statement. Means return statement is optional for void return type.

Example 1:

 
						
                    class University
                    {
                       int institute( )
                       {
                          System.out.println("SDJ founder");
                          return 10;
                       }
                       float sdj( )
                       {
                          System.out.println("praveen");
                          return 10.25f;
                       }
                       void affiliated( )
                       {
                          System.out.println("govt of.india");
                       }
                       public static void main(String args[ ])
                       {
                          University u=new University( );
                          u.institute( );
                          u.sdj();
                          u.affiliated();
                       }
                    } 
                        

   Output:

SDJ founder praveen govt of.india

Example 2:

 
						
                     class A
                     {
                        int i;
                        Double m1( )
                        {
                           double d=10.20;
                           /* return “abc”;  */
                           /* Here compilation fails because String value is not */
                           /* specific to double. Here we are expecting double value */
                           /* but  it is returning String value. */
                           return d;
                        }
                        void m1( )
                        {
                           System.out.println(“SDJ “);
                           /*  return 0;   */
                           /*  Here compilation fails  because  m1 method contains  */
                           /*  a return type  “void”. For void  type there is no return value. */
                        }
                        String str( )
                        {
                           ------;
                           return null;
                        }
                        A m2( )
                        {
                           ------;
                           return new A();
                           /* Here compilation success  because m2 () return type is “A” type */
                           /* and  here it  returns same type object i.e. new  A() .  */
                        }
                     }                        

Example 3:

 
						
                     class University
                     {
                        int institute()
                        {
                           System.out.println("SDJ founder");
                           return 10;
                        }
                        float SDJ()
                        {
                           System.out.println("praveen");
                           return 10.25f;
                        }
                        void affiliated()
                        {
                           System.out.println("govt of india");
                        }
                        public static void main(String args[])
                        {
                           University u=new University();
                           System.out.println(u.institute());
                           System.out.println(u.sdj());
                           /* System.out.println(u.affiliated()); */
                           /* Here compilation fails  because we are  calling void method */
                           /* i.e. affiliated()  inside “System.out.println” . */
                           /* sop is  expecting some value other than void . void means “empty”. */
                        }
                     }                        
                       	

   Output:

SDJ founder 10 praveen 10.25