Friday, June 18, 2010

How main method will execute?


How main method will execute?
Why i am asking this question is, we are not passing any arguments to main method.
If you defined a method, which is taking a String array as parameter. If you want to call that method, you must pass a String array (at least null) to that method. Without passing arguments you will not call that method. In the same way, how the main method is executing without passing any arguments (even not null also).

public class Test {
  public static void main(String args[]) {
    System.out.println("args : " + args);
    System.out.println("args.length : " + args.length);
  }/*main()*/
}/*Test*/



OUT PUT
---------
java Test (press enter)
args : [Ljava.lang.String;@10b62c9
args.length : 0

Here i will give you some explanation for the above question

If you observe the above output, here i am not passing any arguments to main method while execution. JVM will check, whether the programmer is passing any arguments to the main() method or not. If your not passing, then, it will create a String array with 0 (zero) index. Means, a String array without any strings in it.
That is the reason why, its showing args : [Ljava.lang.String;@10b62c9, this is the proof that the args is not null. And its length is 0 (zero).


public class Test {
  public static void main(String args[]) {
    System.out.println("args : " + args);
    System.out.println("args.length : " + args.length);

    if (args.length > 0) {
      for (int index = 0; index < args.length; index++) {
        System.out.println("args[" + index + "] : " + args[index]);
      }/*for-loop*/
    }/*if-state*/
  }/*main()*/
}/*Test*/


OUT PUT
---------
java Test hi Hello(press enter)
args : [Ljava.lang.String;@10b62c9
args.length : 2
args[0] : hi
args[1] : Hello

If you pass any arguments to the main() method, JVM will create String array with the arguments passed by you. These are called command line arguments. You can pass command line arguments as above.

Friday, June 11, 2010

preventing to create objects to your class

We can restrict others to create objects of our classes by defining constructors as private, we can restrict. Even though, we can write some special kind of methods to get object of our classes. Those methods must be static.

        This is similar to java.lang.Runtime class. You will not create object to that class, since java.lang.Runtime class's constructor has private access. So, you will not create object to that class. But you can get object of that class with the help of getRuntime() static method of the same class. By doing this, we can restrict others to read(monitor) or write(change) our resources. This is exactly happening with java.lang.System class.
In System class most of the member are static only (out, in, err, etc). You will not get object of System class, but you can use getProperty(String str), gc(), exit(int) etc methods of that class. Since all are static methods.

Here i am giving one example of that.

public class Product {
  private Product() {
    System.out.println("Hi, i am new object of Product class.");
  }/*Product()*/

  public static Product getProduct() {
    System.out.println("Hi, i am getProduct() method.");
    System.out.println("I will create Product class object.");
    return new Product();
  }/*getProduct()*/

  public String getDirectory() {
    return System.getProperty("user.dir");
  }/*getTime()*/
}/*Product*/

class Test {
  public static void main(String[] args) {
    System.out.println("Hi, i am main() method.");
    Product p = Product.getProduct();
    System.out.println("Current Directory : " + p.getDirectory());
  }/*main()*/
}/*Test*/

Thursday, June 10, 2010

method execution before main method

We can execute some special kind of methods and blocks before main method execution. When program execution started, at that time JVM will initialize static variables, static blocks first, then only it will start static main method execution. So, if we write a static block, JVM will execute that static block first, then after it will execute main method. In that static block you can call only static methods. In this way we can execute some special kind (only static methods) of methods before main method execution.

Here, i am giving one exmple as follows,

public class Test {

  private static int random = 123;

  public static void main(String args[]) {
    System.out.println("Hi, i am main() method.");
    System.out.println("random value from main() method : " + random);
  }/*main()*/

  static {
    System.out.println("Hi, i am static block.");
    System.out.println("random value from static method : " + random);
    random = 345;
    getValue();
  }/*static*/

  private static void getValue() {
    System.out.println("Hi, i am getValue() method.");
    System.out.println("random value from getValue() method : " + random);
    random = 456;
  }/*getValue()*/
}/*class*/

OUT PUT:
Hi, i am static block.
random value from static block : 123
Hi, i am getValue() method.
random value from getValue() method : 345
Hello, I am main() method.
random value from main() method : 456

Thursday, April 15, 2010

RMI example with source code

Here i am giving addition of two number with RMI.
Download RMI application from here, rmi

Tuesday, April 13, 2010

Save a tree, it will saves you.

Sunday, March 28, 2010

Medaram Mela

Medaram mela is the sencond biggest mela in India. In this year i went to Medaram in January-2010. Here i am posting my journey details. You can get some idea about Medaram.

My journey started from Jangaon, Warangal by this bus in the mid-night. APSRTC provided buses from Jangaon to Medaram, we reached Medaram early morning 6'o clock.

Nearly it took 4-5 hours.

Saturday, March 27, 2010

About Me

Hi all,
This is Sreekanth Goud, working as a software engineer at Hyderabad.
To know more details about me click here Sreekanth Goud