Recursion M3
Recursion M3
Recursion
Syntax:
returntype methodname(){
//code to be executed
methodname(); //calling same method
}
Recursion Example : Factorial Number
Output :
Factorial of 5 is: 120
Access Control / Access Modifiers
Two types of modifiers in Java:
1)Access modifiers
2) Non-access modifiers.
Access modifiers
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or
class.
User can change the access level of fields, constructors, methods, and class by applying the access
modifier on it.
Non-access modifiers:
Non-access modifiers provide information about the characteristics of a class, method, or variable
to the JVM.
Types : static, final, abstract, synchronized, native, volatile, transient
Access modifiers
Output : Hello
Public
Output : Hello
Example
Static
The static modifier used for creating class methods and variables.
When a member is declared static, it can be accessed before any objects of its class are created,
and without reference to any object.
Static Variables : The static keyword is used to create variables that will exist independently of any
instances created for the class. Only one copy of the static variable exists regardless of the number of
instances of the class.
Static Methods :Static methods do not use any instance variables of any object of the class they are
defined in. Static methods take all the data from parameters and compute something from those
parameters, with no reference to variables.
Static Variables
Output : Hello
Static Variables and Method
Output : 125
final
final variable :
The final keyword indicates that the specific class cannot be extended or a method cannot be
overridden.
Prevents its contents from being modified. This means that you must initialize a final variable
when it is declared.
For example:
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
final method :
A final method cannot be overridden by any subclasses. The final modifier prevents a method
from being modified in a subclass.
The main intention of making a method final would be that the content of the method should not
be changed by any outsider.
final variable and final method