Java – Sample method in java

After the simple ‘HelloWorld’ program this is going to be easy too.

Method:

A method is a collection of coding which is specified by its name and signature.It is called to perform a specific task.

Code:

public class Cars

{
public static void sampleMethod()     //this is a method which prints some message
 {
        System.out.println(“This is a method”);
 }
public static void main(String args[])
{
    sampleMethod();                             // calling the method sampleMethod
}
}

Note:

  • Here the method modifier is static because everything inside the static field will be static.So to call the method inside the main method ‘public static void main(String args[])’ we defined it as static.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s