• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing arguments from command line

 
lily jose
Greenhorn
Posts: 3
Debian Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
]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
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic