0% found this document useful (0 votes)
2 views

In Java

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

In Java

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

 In Java, every method must specify a return type.

If your method does


not return any value, you should use the void keyword.
Here’s how you can correct your method declaration:

public static void fun1() {


// method implementation
}

Make sure to replace fun1 with whatever your method is supposed to do.
If you intend for the method to return a value, you'll need to specify the
appropriate return type instead of void. For example, if it should return
an int, you would declare it like this:

public static int fun1() {


// method implementation
return someIntegerValue; // ensure you return an int
if that's the return type
}

Here’s the complete syntax for declaring methods in Java, along with
explanations of each component:
General Syntax of a Method
java
Copy code
accessModifier returnType methodName(parameterType
parameterName, ...) {
// method body
// return statement (if returnType is not void)
}
Components Explained
1. Access Modifier: Defines the visibility of the method. Common modifiers
include:
o public: Accessible from anywhere.
o private: Accessible only within the same class.
o protected: Accessible within the same package and subclasses.
o (no modifier): Package-private, accessible only within the same
package.
2. Return Type: Specifies the type of value the method will return.
Common return types include:
o Primitive types (e.g., int, double, char).
o Object types (e.g., String, List<String>).
ovoid: Indicates that the method does not return a value.
3. Method Name: A valid identifier that follows Java naming conventions
(camelCase). It should clearly describe the method’s purpose.
4. Parameters: Optional. A method can accept zero or more parameters.
Each parameter must include a type followed by a name. Multiple
parameters are separated by commas.
5. Method Body: Enclosed in braces {}, this contains the code to be
executed when the method is called.
6. Return Statement: If the method has a return type other than void, it
must contain a return statement that returns a value of the specified
type.
Example of a Complete Method
Here’s a complete example:
java
Copy code
public class Example {

// A method that calculates the sum of two integers


public static int calculateSum(int a, int b) {
int sum = a + b; // calculate the sum
return sum; // return the result
}

// A void method that prints a message


public static void printMessage(String message) {
System.out.println(message); // print the message
}

public static void main(String[] args) {


// Calling the methods
int result = calculateSum(5, 10);
System.out.println("Sum: " + result);

printMessage("Hello, World!");
}
}
Key Points to Remember
 Always specify the return type, even if it’s void.
 Method names should be descriptive and follow Java naming
conventions.
 Use access modifiers appropriately based on the desired visibility.
 If your method has a return type, ensure that every possible code path
returns a value of that type.

You might also like