answersLogoWhite

0

What is method declaration in Java?

Updated: 8/11/2023
User Avatar

Wiki User

15y ago

Best Answer

A method declaration is the heading of a method containing the name of the method, its parameters, and its access level. The method heading in Java is organized as such:

[access keywords] [return type] [method name] ( [parameters separated by commas] )

for instance:

public String toString(); is public (accessible by any class), returns a String, is called toString, and takes no parameters.

Other features could be added to the method declaration for a more specialized method such as static (method could be called without an object of that class), native (implemented using the native code, usually what C has already done, i.e. square root, power etc.).

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

The signature of a method is a combination of method name and parameters which can uniquely identify a method. The signature may also include the package and class to which the method belongs.

The access modifiers, return types, and other modifiers of a method may also be included as part of the signature.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Here is an example of a method declaration (his particular method is to makea java file executable)

public static void main (String args[]) { //method starts here

//Code goes here

} //method ends here

Here's a breakdown of the declaration:-

Public - Required. Defines which other classes / methods can access this method; can be public (accessible by all), private (only code in this class can call this method) and protected (A mix of the two)

Static - Same as making a variable static. This is optional.

void - Required. The return variable. Void means that no data is returned to the statement that called this method. int is for an integer return (if the method performs a calculation and passes the answer back, for example), String is for sending a String back, boolean is if this method determines a true or false condition, etc...

If there is a return type, the Return Var; command must be used within the method, where Var is a variable of the same type as the return type.

main - the name of the method

(String args[]) - The parameters passed to this method; basically, whichever variables you want it to do something with. As with the return value, this can be of any variable type, e.g. int, char, object etc...

Here's a quick example of a method that adds two numbers together and returns the answer:

public int addition (int x, int y) {

int answer = x + y;

return answer;

} //end addition

Hope this helps!

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Declaring a method means - creating a method but with only the method declaration and no method definition. That is, the method will not contain any code or logic inside it.

Ex:

public String getName(){}

The above is a method declaration.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is method declaration in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is inline declaration?

It is a declaration of java class in method body which called "inner class"


Is it possible to define a java static method inside a main method?

Because, the main method is the starting point of the java program and if we need an object of that class even before the main can be invoked, it is not possible. Hence it is declared static so that the JVM Can acess the main method without having to instantiate that particular class


When wil you need throws keyword in java?

The throws keyword will be used in method declaration to signify the fact that, some pieces of code inside the method may throw exceptions that are specified in the method signature.


What is overridnig method and overlording method in java?

There is no such thing as overlording in Java.


Explain the structure of a java program?

The fundamental structure of any Java programme should look like: [package declarations] [import statements] [class declaration] An example is given below: package abc; import java.lang; class Demo { public static void main(String[] args) { Sytem.out.println("Hello! World"); } } //The file containing this class must be named Demo.java


What is the task of the main method in java platform?

It is the method that gets called when a Java application is started.


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


What is function in java terminology?

In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.


What is a method in Java?

A Java method is a sequence of statements. It is comparable to a function, subroutine, or procedure in other languages.


How do you prevent method overriding in java?

Use the word "final" directly preceding your method declaration. The presence of final keyword directs java to ensure that, this particular method would not be overridden by any of its child classes.


What is the purpose of NoMatchException handling method in java?

java exception


Why comparator has equals method?

The Java superclass Object says that all Java objects have an equals method. Thus Comparator has an equals method.