File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes passing arguments  from command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "passing arguments  from command line" Watch "passing arguments  from command line" New topic
Author

passing arguments from command line

lily jose
Greenhorn

Joined: Oct 13, 2011
Posts: 3

]can we pass arguments from command line to a constructor (with parameters)?

I learned that we can print the command line arguments as output like in the below code
class CmndLineArguments {

public static void main(String[] args) {
int length = args.length;
if (length <= 0) {
System.out.println("You need to enter some arguments.");
}
for (int i = 0; i < length; i++) {
System.out.println(args[i]);
}
}
}
but my doubt is how to pass command line arguments to a constructor or a method
please can you explain with an exanple
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7649
    
  19

lily jose wrote:can we pass arguments from command line to a constructor (with parameters)?

Yes. A constructor is just a method, like println().

lily jose wrote:but my doubt is how to pass command line arguments to a constructor or a method
please can you explain with an exanple

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Winston Gutkowski wrote:
lily jose wrote:can we pass arguments from command line to a constructor (with parameters)?

Yes. A constructor is just a method, like println().

I wouldn't say that. There are a lot of similarities (e.g. both can take parameters, both can be overloaded, both can have similar bodies including early return statements) but they are still not the same. For instance, a constructor cannot have a return type, where a method must have one. Also, a constructor always uses constructor chaining which is implicitly added if it wasn't added explicitly, whereas method chaining is optional and is not added automatically. For instance:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7649
    
  19

Rob Spoor wrote:I wouldn't say that. There are a lot of similarities...

You're right. I was speaking from the point of view of OP's question which was "can we pass parameters from the command line to a constructor?"; and since her original post included a call to println() that did exactly that, I just meant that the mechanism is the same.

But you're dead right; 'best not to confuse.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing arguments from command line