• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing arguments from command line

 
Greenhorn
Posts: 3
Objective C Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic