Thursday, May 3, 2012

Attitude Towards Technology

Looks strange, in the world where people do not have time even for other human beings, attitude towards technology is a hard to digest term. Lack of time is a great blessing as internet is full of codes and other related information. One can always download, edit and personalize the codes and use it. To be at the top in shortest span (like shortest path J) is the driving force which leads one not to have learning attitude.

Learning sometimes, rather most of the times, is a challenge to existing mindset. We feel there is age slab for learning, say from 4 years to maximum 30 years. Once one is familiar and comfortable with one technology, one approaches the other technology with the same mindset. That is a problematic approach, as every technology is created as a solution to certain problem set. Being a trainer, I face many such questions. When one decides to learn some technology, the groundwork is a must.
Learning attitude is different.

I already know is one of the most common barriers in the learning process. One may have learn the subject in college or worked on it for some time. Sitting and listening is boring and waste of time and reception is nullified. Rather than learning, mind is working on weaving questions which may be prejudiced. It may lead one to become inflexible and rigid rather responsive and thoughtful. To have a good relationship with technology helps being well versed and writing magical codes which may do wonders (optimized and performance oriented).

Having patience to listen and being thoughtful in vertical and horizontal directions can help mastering the technology. Learning the technology to its core, what it provides, why it provides and how does the stuff works, where all it can be applied can bring real joy.  Right amount of curiosity with good analytical tendencies acts as a catalyst. 

Do you think learning in a different way will change your questions? Do you think changing a question can change the life of a coder?

Learning attitude is an art, which helps not only in technology but in handling one’s life too.

Writ : 10/25/2010

Monday, April 30, 2012

How can I count the number of methods in a Java class ?

Here it is a simple solution for the same. You can print the count and method headers with this code.

import java.lang.reflect.Method;
/**
 *
 * @author K.A.N. Nancy
 */
public class CountClassMethods {
    public static void main(String args[]){
        Class className=null;
       try{
          className= Class.forName(args[0]);
          Method[] methods= className.getMethods();
           System.out.println("Number of methods in "+className+" = "+methods.length);
           for(int i=0;i<methods.length;i++){
               System.out.println(methods[i]);
           }
       }catch(ClassNotFoundException classNotFoundException){
           System.out.println("Class "+className+" could not be found");
       }

    }

Coded @ Feb 29, 2012 -  4:10 am IST

Why main() method in Java is static?

Java is strictly Object-Oriented language. Java does not support open functions like C and C++. In Java data and methods are written inside a class. Like C and C++ java application’s entry point is main() method.  main() method is coded inside a Starter class. This is an interesting scenario  . A method of a class can be invoked using its object reference. Now the question is where will you create this object?  object can be created inside another  class e.g.
public class A {
      public void sayHello() {
              System.out.println(“Hello from Java”);
      }
}

To test class A, I create a StartA class.
public  class StartA {
      public void main(String args[]) { // main needs to be public, as will be invoked from outside the class
               A ob = new A();
               ob.sayHello();
      }
}
To call sayHello() method
Class A’s object is created in StatrtA class.  sayHello() method is called by its object reference of class A.   Every application’s execution start from main().  To call main(), an object of StartA class needs to be created. To create an object of StartA class, another class is needed… . So,  create another class to create an object of StartA class, then where can the object be created for that class. Create another class for that and then what next? It will be never ending story.  In this black box, make  a hole and let the main () method slip out. This hole can be created by declaring main() as static method.  A static member belongs to a class and not to the object. By declaring main() method as static, JVM can call main() by its class name i.e StartA.main().  Java’s beauty is in its simplicity.
 StartA class can be coded as :
public  class StartA {
      public static void main(String args[]) { // main needs to be static to be called by its class name
               A ob = new A();
               ob.sayHello();
      }
}