Saturday, 16 February 2013

Java Basics For Oracle Apps Developers 1

Hi All,
My work, personal commitments and lazyness have been trying to kill my blog. I could somehow find time and post another one ;) Hopefully you will like it. I am also trying to create a Category of Blogs "Chapter by Chapter" and link the posts properly so that someone can go in Order. Another would be a Random one where I will Post what I learn so we learn together!!!!

Come Oracle Fusion for e-business Suite; Oracle Apps developers will have to start developing on ADF. The first hurdle which Oracle Apps developers face is Java. PL/SQL, is a procedural language and is relatively simple; so the technological upgrade seems overwhelming and frightening.

Well don't worry, you don't need to be an expert java developer, all you need is Basic knowledge of Core Java. ADF shields you to a great level but you still need to know the Basics. The idea behind this post is not to make you a Java Developer but to enable you with basic knowledge of Java to aid in ADF development.

This post designed to take you from the Hello World (well I have used another program) and some fundamental concepts of Java to enable you to do ADF development.

I am not going to talk about theories of OOPS and Java which generally tend to confuse people. Lets just dive in without worrying too much.

Overview: Creation of Classes and using making use of them through Objects.
Duration: 30 Mins

This is what you will require to do most of the time so you must be familiar with these concepts.
  1. Goto File New in JDeveloper and chose Generic Application.
  2. In the next screen give application name as "JavaTutorial" and Package as "com.tutorial".
  3. Press next and give Project as "AnimalProject" and suttle Java to the right hand side.
  4. Press Next and Finish. Once its done it will look like following.
  5. Now lets start creating Classes and do the real stuff. One can create a Class by Right Click on Project -> New -> General -> Java -> Java Class or from JavaTutorial Overview -> Java Files -> Add (Green Button).

    Don't check Constructor, Abstract Method and Main method and start with a very simple Class. 


  6. You can directly overwrite from below
     
package com.tutorial;

public class Animal {
    /**
     * Prints "Animals move"
     */
    public void move() {
        System.out.println("Animals move");
    }

    /**
     * Finds animal from Sound it makes.
     * Pass Sound name e.g pass Bark and it will return Dog
     */

    public String findAnimalFromSound(String sound) {
        if (sound.equalsIgnoreCase("Bark")) {
            return "Dog";
        } else if (sound.equalsIgnoreCase("Roar")) {
            return "Lion";
        } else
            return "Sorry! Could not identify the sound. Try another search!!";
    }
    
    /**
     * Finds Sounds from Sound it makes.
     * Pass Animal name and it will return the sound it makes
     */

    public String findSoundAnimalMake(String animalName) {
        if (animalName.equalsIgnoreCase("Dog")) {
            return "Bark";
        } else if (animalName.equalsIgnoreCase("Lion")) {
            return "Roar";
        } else
            return "Sorry! Animal not identified. Try another search!!";
    }
}
So, lets analyze the class and some fundamentals

  • Name of the file should be same as that of class name.We created a class names Animal in package com.tutorial.There will be a file with same name Animal.java in the file system. To confirm Goto Tools -> External Tools -> Select -> All and OK.


    At this time we are particularly concerned with "Explore Directory" which will take you to file on file system. Right click on Animal.java in the Navigator and click on "Explore Directory". It will take you to Animal.java on file system. This confirms that the java file name is same as the class name. At this point if you want to change the class name ADF will show an error.  
      • A Java file can have multiple classes but for all the practical purposes lets just say that it will only have one class. JDeveloper will prompt a warning if you try to add another class.
        • A class can have many methods (similar to Oracle PL/SQL functions), which may or maynot return any value back to calling function. In this case, we have three methods
          • move which just prints "Animals Move". The return type is void which means it will not return anything. There is no input arguments (also called parameters)
          • findAnimalFromSound which takes one argument of type String and returns String. As the name suggests this method will find the Animal if you pass the sound.
          • findSoundFromAnimal which is simlar to above but the functionality differs as it returns the name of Sound Animals make
          • System.out.println is equivalent to dbms_output.put_line and is very useful for quick debugging (Though ADF provides excellent debugging framework out of the box).
            • Something about code comments: There are mainly three types of comments one can put in java code.
              • Single line (by using // at begining of the line). This is used for commenting single line
              • Multi-line (Start with /* and end with */). Whatever comes in between /* and */ is commented.
              • Documentation comment Starts with /** and ends with */ . This is important to use this comment at the starting of each method. One can use Multi-line as well; but using documentation on the methods will make sure that when you are using the method in another class; ADF will display your comments. We will see that in a bit when we create another class TestAnimals.java to test the functionality of class "Animals.java".
              • All methods are also declared public meaning it can be called from anywhere.

              So, can we run this class. NO; not just yet. Execution in Java begins with main method; since there is no main method we cannot run this. To see the use lets create another class TestAnimals.java.

              Just like earlier Right Click on com.tutorial and create Java class. This time give name as TestAnimals.java

              You may like to add the following code

              package com.tutorial;
              
              public class TestAnimals{
                  public static void main(String[] args) {
              
              // Instance of Animal Class (Object a) to Get Animal Name from sound
                      Animal a = new Animal();
                      a.move();
                   
                      String animal=a.findAnimalFromSound("ROar");
                      System.out.println("Roar = Sound of "+animal);
                      
                      animal = a.findAnimalFromSound("bark");
                      System.out.println("Bark = Sound of "+animal);
                      
                      animal = a.findAnimalFromSound("Chirp");
                      System.out.println("Chirp = Sound of "+animal);
              
              // Another instance of Animal (Object b) to Get Sound From Animal example
                      Animal b = new Animal();
              
                      String sound=b.findSoundAnimalMake("Lion");
                      System.out.println("Lion's Sound: "+sound);
                      
                      sound = b.findSoundAnimalMake("Dog");
                      System.out.println("Dog's Sound: "+sound);
                      
                      sound = b.findSoundAnimalMake("Bird");
                      System.out.println("Bird's Sound:"+sound);
                      
                  }
              }
              

              You might have heard people taking about Object and class in Java.
              Where does object come into picture?
              Simply put "Object is an Instance" of a class. In the example above Animal is a class and variable "a" and "b" are the Objects of the class. So a and b will be the Objects.

              Objects are create like below
                      Animal a = new Animal();
              Its a good practice to seperate the declaration and assignment like the below
                      Animal a;
                      a = new Animal();

              The statement "Animal a;" declares a object of type Animal. But it will not have any memory assigned to it. That will only be done with a = new Animal();

              Right click and run TestAnimals.java and you will see the results just below the code.



              Oh yeah about Documentation comments. Just goto TestAnimals and type "b." and chose any method from Animals class; you will see the documents right up there. Dont have to goto Animals.java. See below. I think its cool and good pratice to include Documentation Comments.


              This conclude our first Java Tutorial; this was pretty easy but its very important that you get a hang of things (try to type rather than just copying the code).

              I will post another one on Core Java explaining some other concepts (ofcourse with examples) like Method Overloading, Overriding etc.. and then we will be good to go.

              Until then happy Coding.