Java Method is very much similar to a normal method or a function written within a class definition. Java method is basically a set of statements group together to perform a specific task. This method is included in a class. An object instantiated from a class can call methods of that class.
Every Java method has a basic structure which must be followed to create any type of method. This structure of a Java method holds the following components:
- Method Header [Modifiers + Return Value Type + Method Name + List of Parameters]
- Method Body [Programming Statements + Return Value]
- Modifiers define the access level and type of method.
Ex: Public, Private, Protected, Static, Final - Return Value Type states what type of value will a method return after execution. If a method does not return anything, then the void keyword is used.
Ex: Int, Void, String - Method Name is the name of a method. This name is then used to call a method from outside or within a class. A method name cannot have white-spaces.
- List of Parameters are variables passed to a method to perform some action on them.
- Programming Statements are a set of execution statements group together to perform a specific task.
- If a Return Value Type is defined in a Method Header, only then can a method return any value of that type.
A Java method can be of different types:
- Final Method
- Public Method
- Private Method
- Package Method
- Instance Method
- Protected Method
- Static Method
- Main Method
The Main Method
Every Java program must have one main method. The main method is the first method which the Java Virtual Machine executes. The main method then calls the other methods, as they are needed. Main method is often written as main() method.
Here is the main() method from a Java program that prints “Hello World:”
public static void main( String[] args ) { // This Java program prints "Hello World!" System.out.println( "Hello World!" ); }
It is good programming practice to maintain a very small main method and to move all possible code into other methods.
Instance Method
An Instance method is associated with a single object only and uses instance variables/methods of that class instance. Unlike an Instance method, Static method is no bound to a single object. Multiple instances of a same class can use the static method. A method automatically becomes an Instance method if no qualifier is defined.
Static Method
A Static Java method is shared across multiple instances of a class. It is not bound to any specific instance. Class variables cannot be used within a Static method, unless the class variables are also defined static.
The most common way to pass data to a Static method is via method parameters. These methods are generally used for simple calculations or processing.
To define a method as Static, you should prefix it with a keyword static as shown below:
Ex: public static int add( int A, int B ) { }
Package Method
A package method can be called by any class within its package. This is the default access level if no access level is explicity declared. The other options for declaring visibility are private, public, and protected.
Private Method
A private method is a method which is not inherited by subclasses. Declaring a method private defines its access level. The other options for declaring visibility are public and protected.
Therefore, a private method can only be called by other methods of the same class. A sub-class cannot access a private method of its parent class. If undeclared, the default access level is package.
Ex: private void my_method_name() { }
Protected Method
A protected method can be called by any subclass within its class, but not by other disconnected classes. Declaring a method protected defines its access level. The other options for declaring visibility are private and public.
Therefore, a protected method can be accessed from within a class and all its sub-classes (a sub-class shouldn’t override it with a private access qualifier). Protected methods are generally used to secure access and keep it within the boundaries of inheritance.
Ex: protected void my_method_name() { }
Public Method
A public method is a method which can be called by any object. Declaring a method public defines its access level. The other options for declaring visibility are private and protected.
It is advisable to define appropriate access qualifiers and keep Public as the last choice. Because a public method can be accessed from anywhere, it could become vulnerable to malicious activities.
Ex: public void my_method_name() { }
Final Method
A final method is a method which cannot be overridden by a subclass. It is particularly used to protect the method from being overridden within its sub-classes.
Ex: public final void my_method_name() { }
Koichi Yagi
I am not quite sure about Package Method. I you can explain more
about the Package Method, it will help me in my computer classes.
I am currently taking a class re: JAVA language.
lol
hmmm… your main method has a problem…
it should be
System.out.println(“Hello World”);
not
System.out.println{“Hello World”);
btw any examples on instance methods?