Search This Blog

Thursday, May 3, 2012

Attitude Towards Technology

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

Learning, sometimes, rather than most of the time, is a challenge to the existing mindset. We assume that there is an age for learning. 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 a certain problem set. As a trainer, I face many such questions. When one decides to learn a particular technology, the groundwork is required. Learning attitude needs to be 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 a sheer waste of time, and reception is nullified. Rather than learning, the mind is working on weaving questions that may be prejudiced. It may lead one to become inflexible and rigid rather than 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 one master the technology. Learning the technology to its core, what it provides, why it provides, how the code works, and where it can be applied brings the joy of knowing.  The right amount of curiosity with good analytical tendencies acts as a catalyst in the learning process. 

Do you think learning differently will change your questions? Do you think changing a question can change the way one will code?

Learning attitude is an art that helps not only in technology but also in handling one’s life.

By Nancy K A N
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 a 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 applications’ entry point is the main method. The 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?  An 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.  The sayHello() method is called by the object reference of class A.   Every application’s execution starts from main().  To call main(), an object of the StartA class needs to be created. To create an object of the StartA class, another class is needed…. So,  create another class to create an object of the StartA class, then where can the object be created for that class? Create another class for that, and then what next? It will be a 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 a static method.  A static member belongs to a class and not to the object. By declaring the main() method as static, the JVM can call the 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();
      }
}