Java is one of the prominent and widely used programming languages and the methods and variables in java make the programming easier.
A method in java is a set of codes called by a name which can be invoked at any point in a program with a name assigned to it. A method in java can also be defined as a set of statements clubbed together to perform a specific action. However, methods are set out in a particular way. A method header and a method body will be there. At method header, you should mention the value, if any of the methods is supposed to return. Name of the method should also be included in the header. The codes will go to the method body.
Basically, there are two types of methods in Java, regular or nonstatic methods and static methods. The major difference between these two methods is Object Oriented Programming (OOP) language. You do need to have an object to access static method whereas a nonstatic method is always invoked or called by an object or a class.
Once created, methods should be called or invoked to use them. Here you will find two different ways for calling a method. Methods either returns a value or a nil value. When a method is called the program control will be transferred to the called method. The control will be handed over to the caller once the return statement is executed and once it has reached the method ending closing brace.
Variables in Java
Just like methods variables also help to program with ease when you use Java as the programming language. Variable is a piece of memory that contains a data value and variables in java are used for storing information which is necessary for the java program to finish the task.
Each variable in Java has a specific type that defines the size and layout of the memory of the variable. Similarly, the range of values which can be stored and set of operations possible to be applied will be different from variable to variable.
Basically, there are three types of variables in Java and they are
- Local Variables
- Instance Variables
- Class or Static Variable
Local variables will be declared in methods, constructors or blocks while Instance Variables will be declared outside the methods, constructors or blocks but in a class. Class Variables which are more commonly known as Static Variable are declared in a class but with a static keyword.
This is just an introduction to two major aspects of Java, methods, and variables. These will be taught in detail in java tutorials and should be learned thoroughly to master java programming language.