Methods in Java

Edu Tech Learner
0


 

Methods in Java are blocks of code that perform a specific task or operation. They are used to achieve the reusability, modifiability and readability of code. Methods can be classified into two types: 

  1. Predefined 
  2. User-defined.

1.Predefined methods are the methods that are already defined in the Java class libraries or the Java API. They are also called built-in or standard methods. For example, System.out.println() is a predefined method that prints a message to the standard output stream. Predefined methods can be accessed by using the dot (.) operator on the class name or the object name.

2.User-defined methods are the methods that are created by the programmer to perform a custom task or operation. They are also called custom or programmer-defined methods. For example, public static void myMethod() is a user-defined method that can be invoked by using the method name followed by parentheses (). User-defined methods can be declared and defined inside a class.

Methods can also be categorized based on their modifiers, parameters and return types. For example, static methods are the methods that belong to the class and not to any object of the class. They can be accessed without creating an object of the class. Non-static methods are the methods that belong to an object of the class and can only be accessed by creating an object of the class.

Methods can have zero or more parameters that are passed as arguments when the method is called. Parameters are used to pass data or information to the method. Methods can also have a return type that specifies what kind of value the method returns after executing its statements. If a method does not return any value, it has a void return type.

The syntax for declaring a method in Java is as follows:

modifier return_type method_name(parameter_list) { // method body }

Here, modifier is an optional keyword that specifies the visibility and behavior of the method, such as public, private, static, final, etc. return_type is a keyword or a class name that specifies what kind of value the method returns, such as int, double, String, void, etc. method_name is an identifier that follows the naming convention of Java and represents the name of the method. parameter_list is an optional list of parameters separated by commas that specifies what kind of data or information the method accepts, such as int x, double y, String name, etc. The parameter_list is enclosed within parentheses (). The method body is a block of code enclosed within curly braces {} that contains the statements or instructions that define what the method does.

The syntax for calling a method in Java is as follows:

method_name(argument_list);

or

object_name.method_name(argument_list);

or

class_name.method_name(argument_list);

Here, method_name is the name of the method that is being called. argument_list is an optional list of arguments separated by commas that matches with the parameter_list of the method declaration. The argument_list is enclosed within parentheses (). object_name is an identifier that represents an object of the class that contains the method. class_name is an identifier that represents the name of the class that contains the method.

Post a Comment

0Comments
Post a Comment (0)