开发者

Passing arguments in main method in java class

开发者 https://www.devze.com 2023-01-26 14:16 出处:网络
Can someone tell me what is the need to declare a class like this: public class Test { 开发者_如何学编程

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?


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.


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).


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.


The main method is the entry point for the program and is called when you run java Test from the command line.

public Test(String a, String b, String c) is a public constructor for the Test class and is called when you call new Test(l,m,n); Note that a in the constructor and l in main method refer to the same String... this also applies to b and m; c and n.

As a side note, this class expects to be passed three values from the command line, and then stores them in l, m, and n

One last note: If you have a method with the signature public void run(), your class should likely implement Runnable so that it can be used in a Thread.


Learn Java.

A constructor is a function that gets called to create an object, and it's denoted by a function with the same name as the class, but no return type. Multiple constructors can be declared with different arguments.

In this case, the arguments are taken out of the argument array and passed as arguments to the constructor for Test.

These are fundamentally basic concepts to the Java programming language. You should read up on Java. Try Thinking in Java, this is a great book to get started.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号