provide objectname as command line argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpr
    New Member
    • Feb 2008
    • 11

    provide objectname as command line argument

    I am new programmer of java. I believe someone can help me out with following problem
    [CODE=cpp]class Employee
    {
    string name1;
    Employee()
    {
    Name="Amitabh";
    }
    Employee(String s)
    {
    Name=s;
    }
    public static void main(String[] args)
    {
    ???
    }
    }[/CODE]

    if I am running the above application as:
    java Employee Mayuri

    Then is it possible to initialize Mayuri(i.e. args[0]) as an object of class Employee?
    I mean to say Is it possible to provide objectname as command line argument?
    Last edited by Ganon11; Feb 5 '08, 03:25 AM. Reason: Please use the [CODE] tags provided.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    [CODE=Java]Employee emp = new Employee(args[0]);[/CODE]

    Comment

    • jpr
      New Member
      • Feb 2008
      • 11

      #3
      Originally posted by BigDaddyLH
      [CODE=Java]Employee emp = new Employee(args[0]);[/CODE]
      If I a not wrong above code is creating emp as an object of class Employee but I want to create args[0] as an object of class Employee.
      Is it possible?

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by jpr
        If I a not wrong above code is creating emp as an object of class Employee but I want to create args[0] as an object of class Employee.
        Is it possible?
        I'm not sure I understand. You want the name of the variable to be the value of the given string args[0]? So if args[0] is "billgates" , you want to define a variable:

        [CODE=Java]Employee billgates;[/CODE]

        Comment

        • jpr
          New Member
          • Feb 2008
          • 11

          #5
          Originally posted by BigDaddyLH
          I'm not sure I understand. You want the name of the variable to be the value of the given string args[0]? So if args[0] is "billgates" , you want to define a variable:

          [CODE=Java]Employee billgates;[/CODE]
          Right, I want to define value of args[0] i.e. billgates(in case of your example) as object of class Employee.

          so plz tell me is it possible?

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by jpr
            Right, I want to define value of args[0] i.e. billgates(in case of your example) as object of class Employee.

            so plz tell me is it possible?
            The short answer is no.

            An alternative approach would be to define a java.util.Map and use the string as a key.

            I can't say more because I have no idea what you are trying to do or why you want to name variables after strings.

            Comment

            • hollywood115
              New Member
              • Feb 2008
              • 16

              #7
              yea i agree with the guy above.. i dunno why you would wanna name your variables after strings.. it would make things very hard to keep track of.. ud be better of naming it urself or using an array if ur grabbing lots of employees... its a lot easier for u as a programmer to keep track of the variables that way.

              Comment

              Working...