You are on page 1of 1

Methods Methods are blocks of code with a name containining the executable code in Java.

Methods can optionally accept data as parameters and also optionally return dat a. Executing a Java program means executing its methods. We will initially call one method and then that method can call another method and so on. When you exec ute a java class (using java command) in console, JVM calls a predefined method (the main method) of this class, this method can then call another method of the same class or different class and so on. public static void main(String[] args) { //method body } public - is the access specifier that tells how visible the method is within and outside the class. static - this is a static method, not an instance method. void - return type. void means nothing is returned. main - name of the method. args - A String array to accept command line arguments. Methods will always be contained within a class. The visibility of a method is c ontrolled by its access modifiers public, private, protected and default (no acc ess modifier). Methods may be either static or instance. Static methods can acc ess only static variables, but instance methods can access static as well as non -static methods. Static methods can be called using a class name as well as obje ct name, instance methods can be called only using an instance.Regardless of the type of method, there is only a single copy of a method. Each instance of the c lass (an object) uses the same definition of the method.

You might also like