Menu

Recursive method in Java

How to write a recursive method in Java?

Below is the code snippet of a recursive method in Java to generate numbers and check the number and execute the method again. In the below example printNumber() is a recursive method that will get called inside its own body.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package rashidjorvee;

public class RecursiveMethod {

 public static void main(String[] args) {

  RecursiveMethod recursiveMethod=new RecursiveMethod();
  recursiveMethod.printNumber(2);

 }
 private void printNumber(int j) {
  try {
   int i=j; 
    if(i==2) {
     printNumber(4);
    }
    System.out.println("Number "+i);
    }
  catch(Exception e) {

  }
 }
}

List of all the AEM components

List of all the AEM component and detail of that component and where all this component is being used.

First, navigate to the components path using the below URI.
http://localhost:4502/libs/wcm/core/content/sites/components.html

From right-hand side apply the filters on the basis of site path and group to get the filtered list of components.
http://localhost:4502/libs/wcm/core/content/sites/components.html
Components in AEM

From the filtered list select your component or click on the name of the component to open the detail of that component in AEM.


Now move to Live usage tab to view the list of pages where this component is dropped.


How to create session from resourceResolver object?

Sometimes we need a session to work with some special JCR API which we can,t use with the help of resourceResolver then we can create an instance of a session in the same class file using the object of resourceResolver. Below is the code snippet.

First, we need to inject the ResourceResolverFactory interface to create a new resourceResolver in AEM. You could inject using the SCR annotation @Reference or Sling Model annotation @Inject.
@Reference
private ResourceResolverFactory resolverFactory;
Now create the ResourceResolver object using resourceResolverFactory. This line of code will create a rsourceResolver with administrator login.
ResourceResolver resourceResolver=resolverFactory.getServiceResourceResolver(null);
Now will create the instance of Session using the object of resourceResolver.
Session session = resourceResolver.adaptTo(Session.class);