Tuesday, 23 June 2015

Methods in JAVA

Methods:
       
 Functions in C++ are called as methods in JAVA.Methods  do have some specifications.There are three parts of methods.They are

  • Method declaration
  • Method definition
  • Method calling/Invoking
Method declaration is not possible.Programmer can directly define and call their method.There are some built-in methods available as in other programming languages.

Sample JAVA program using methods:

Class Myclass
{
          public void hello()
          {
                 System.out.println("First program with a method");
          }
}
Class Mainclass
{
          public static void main(String[] args)
          {
                Myclass c=new Myclass();
                c.hello();     
          }
}

In this program,there are two classes.when you save your program you should save with name of class in which the main function is residing.

Static Functions:
            It is possible to create a static functions in JAVA.Static functions are those functios  which can be called without using objects.

Example of a static functions:

Class Myclass
{
          public void hello()
          {
                 System.out.println("First program with a  static method");
          }
          public static void main(String[] args)
          {
                hello();     
          }
}

No comments:

Post a Comment