How to Write a Method in Java

Java is the most popular programming language, primarily because of its portability. The same Java program can be used on virtually any platform because the source is compiled to byte code. Each machine then converts the byte code into native machine language. Java syntax will be generally familiar to C++ programmers and functions are written similarly. The following steps will show how to write a method in Java.

Instructions

    • 1

      Learn the basic syntax of a Java function:

      () {

      }

    • 2

      Specify the data type of the return value for where you see . The method body may return a value to the calling program using the return statement. Use void for the return type if no value will be returned.

    • 3

      Provide the method name after the return type. The signature of the method must be unique within that scope. The signature of the method is the combination of its name and parameter types.

    • 4

      List the parameters in the parameter list. This list consists of a series of data types and parameter names separated by commas. The parameter list is optional. The method body is a series of Java statements enclosed by braces.

    • 5

      Look at the following example of a Java method that finds the product of two integers:

      int product(int a, int b) {
      int c=a*b;
      return c;
      }

      Notice how the return type is int and the method name is product. This method has a parameter list consisting of two integers. The method body multiplies the two input parameters together and returns the product.

Related Searches:

Resources

Comments

View all 8 Comments

You May Also Like

  • How to Write Java Script

    JavaScript is the common name that refers to the ISO standard ECMAScript, which is used for client side programming for web pages....

  • How to Write an Abstract Class in Java

    In Java, abstract classes allow you to define classes that are not actually instantiated themselves, but other classes can extend. These other...

  • How to Create a Calendar in Java

    Java's calendar class stores information about a moment in time. The class can be used to compare with different times, to manipulate...

  • How to Make a GUI in Java

    Java allows developers to create graphic user interfaces (GUIs) from a toolkit called "Swing." Swing includes many packages that allow the implementation...

  • What Is a Constructor Method in Java?

    Java is an object-oriented programming language used to create applications in one environment that can run in another environment without needing to...

  • How to Write to a file with JAVA

    The task of writing to a file with Java is greatly simplified with Input/Output streams. These are a group of classes used...

  • Java Random Number Method

    Random Number methods in Java return random numbers for use in the program. There are two ways to generate a random number...

  • Method for Writing a Screenplay

    A screenplay must follow an exact formula and use specific techniques before any valid production company will take it seriously. Most screenplays...

  • How to Create a Date From a String in Java

    Creating a Date object from a string in Java is a trivial function when using the DateFormat class. The DateFormat class contains...

  • Get and Set Method in Java

    Java is an object-oriented programming (OOP) language. Object-oriented code is organized using a construct called a "class," which serves as a blueprint...

  • How to Delay a Function in Java

    In the Java programming language, a function may need to delay any further processing during a given amount of time. Because different...

  • How To Write Java Programs

    Java is an object-oriented programming language originally developed and released by Sun Microsystems in the mid-1990s. Java was developed from the start...

  • How to Write the Methods Section for a Research Paper

    The methods section of a research project describes how your specific research project was carried out. This article includes steps to writing...

  • How to Use a Heapsort in Java

    Sift large values towards the root of the tree and small values toward the leaves with the siftDown method. As this method...

  • How to Write RFT Scripts in Java

    RFT, or Rational Function Tester, is an automation tool that is used to test automation functions in computer programming. It can be...

Related Ads

Featured