Passing arguments in main method in java class

80,210

Solution 1

The public Test(...) is a constructor and its purpose is for object creation. This is clearly seen from the sample code...

Test obj = new Test(l,m,n);

The variable obj is instantiated with object Test by being assigned to the Test's constructor. In java, every constructor must have the exact same name (and case) as the java file it's written in (In your case constructor Test is found in Test.java).

...Why is it like this?

It all depends on what you want to do with your object. You could have a zero-argument constructor (i.e. requires no parameters) and have methods to set your l, m, n, like so:

package net;


public class Test {

    private String k;

    /**
     * 
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void set(String a, String b, String c) {
         k = a + " " + b + " " + c; //do something
    }

    public void run() {
        System.out.println(k);
    }

    public static void main(String[] args) {
        String l = args[0];
        String m = args[1];
        String n = args[2];
        Test obj = new Test();
        obj.set(l, m, n);
        obj.run();
    }
}

As you can see, it's exactly the same feature as your example but with a zero-argument constructor.

If your class has no constructor at all, java adds a public zero-argument constructor for you automatically.

Hope this helps.

Solution 2

The method called Test is a so-called constructor for the Test class. The constructor is the method that gets called when you write something like new Test(...).

Bear in mind that the main method is a static method, which means that it does not require an instance of the class Test to be called. This is not the case for the run method. run is an instance method, and to invoke it you need an instance of the Test class (the obj in your case).

Solution 3

The public Test(...) bit is the constructor of that class. It always has the same name as the class. Classes and main methods are two quite different aspects of programming. Classes define reusable components that have both state and methods. The main method is a special method that gets called from the command line.

Your example is so trivial that it doesnt really show any benefits of Object Orientated Programming. If you consider an example where you had different Classes intetracting you might get more of a feel for it.

Share:
80,210
Rizwan
Author by

Rizwan

Updated on February 17, 2020

Comments

  • Rizwan
    Rizwan over 4 years

    Can someone tell me what is the need to declare a class like this:

    public class Test {
    
     String k;
     public Test(String a, String b, String c){
      k = a + " " + b + " " + c; //do something
    
     }
    
     public void run(){
      System.out.println(k);
     }
    
     public static void main(String[] args) {
      String l = args[0];
      String m = args[1];
      String n = args[2];
      Test obj = new Test(l,m,n);
      obj.run();
     }
    
    }
    

    Of course it works but I don't get the point why would one use such way to implement something. Is it because we need to pass arguments directly to the class main method that is why we use this way or is there some other reason?

    What is the purpose of public Test(...) using the same class name. Why is it like this?

  • Rizwan
    Rizwan over 13 years
    thanks Klaus, but Im just wondering why would one use this approach when a simple class like below can accomplish the task: public class Test{ static String k=""; public static void run(){ System.out.println(k); } public static void main(String[] args) { String l = args[0]; String m = args[1]; String n = args[2]; k = l+m+n; run(); } }
  • Rizwan
    Rizwan over 13 years
    Thanks, your example says it all.
  • Klaus Byskov Pedersen
    Klaus Byskov Pedersen over 13 years
    @Rizwan, there are many ways to accomplish the same thing in programming. Why someone would choose to do it like that could either be because of personal preference, because it's the only way they know, etc. For a program like that, that has no real value, I don't think it's important to discuss which approach is best.