0% found this document useful (0 votes)
45 views16 pages

Recursion M3

The document discusses various Java concepts including recursion, access modifiers, and static and final modifiers. It provides examples and explanations of each concept. Recursion is when a method calls itself continuously. Access modifiers specify the accessibility or scope of classes, methods, and variables. There are four access modifiers in Java - default, private, protected, and public - that control where elements can be accessed from. Static modifiers create class methods and variables that are independent of any object and can be accessed without creating an instance. There is only one copy shared among all objects. Final modifiers prevent classes from being extended, variables from being modified, and methods from being overridden.

Uploaded by

V Puneeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views16 pages

Recursion M3

The document discusses various Java concepts including recursion, access modifiers, and static and final modifiers. It provides examples and explanations of each concept. Recursion is when a method calls itself continuously. Access modifiers specify the accessibility or scope of classes, methods, and variables. There are four access modifiers in Java - default, private, protected, and public - that control where elements can be accessed from. Static modifiers create class methods and variables that are independent of any object and can be accessed without creating an instance. There is only one copy shared among all objects. Final modifiers prevent classes from being extended, variables from being modified, and methods from being overridden.

Uploaded by

V Puneeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

A Closer Look at Methods and classes

 Recursion

 Access control /Access modifier


Recursion
 It is a process in which a method calls itself continuously. A method in java that calls itself is
called recursive method.

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 

4 types of Java access modifiers


1) Default: The access level of a default modifier is only within the class. It cannot be accessed from
outside the class. If you do not specify any access level, it will be the default.
2)Private: The access level of a private modifier is only within the class. It cannot be accessed from
outside the class.
3) Protected: The access level of a protected modifier is within the package and outside the package
through child class. If you do not make the child class, it cannot be accessed from outside the
package.
4) Public: The access level of a public modifier is everywhere. It can be accessed from within the
class, outside the class, within the package and outside the package.
Default

  Created two packages pack and mypack .


 We are accessing the A class from outside its package, since A class is not public, so
it cannot be accessed from outside the package.
Private

 Two classes A and Simple.


 A class contains private data member and private method. We are accessing these private
members from outside the class, so there is a compile-time error.
Protected

 created the two packages pack and mypack.


 The A class of pack package is public, so can be accessed from outside the package. But msg
method of this package is declared as protected, so it can be accessed from outside the class only
through inheritance.

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’ members can be accessed without creating an object

 When ‘static’ is applied to a member variable, it makes it a ‘global variable’

 There will be only one copy of the ‘global variable’

 All objects of the class share the same ‘global variable’

 Static variables can be initialized in a ‘static block’


Static
 When ‘static’ is applied to a member method, 3 restrictions are applied to it:

✔ Static methods can only call other static methods directly

✔ Static methods can access only static member variables

✔ Static methods cannot use ‘this’ or ‘super’ keyword

 We can declare both methods and variables to be static.

 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

You might also like