Results 1 to 7 of 7
  1. #1
    Mark Robert is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Default Arguments

    Hi, how can I create default arguments in java like in C++?
    e.g., void MethodName( int a = 1, int b = 1 );
    This way I can call that function with/without specifying arguments.

    One more thing, how can I create functions outside a class? Is that possible? Please help..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Mark Robert View Post
    Hi, how can I create default arguments in java like in C++?
    e.g., void MethodName( int a = 1, int b = 1 );
    This way I can call that function with/without specifying arguments.
    Use method overloading:

    Java Code:
    void methodName() { methodName(1, 1); }
    void methodName(int a) { methodName(a, 1); }
    void methodName(int a, int b) { ... }
    Quote Originally Posted by Mark Robert View Post
    One more thing, how can I create functions outside a class? Is that possible? Please help..

    You can't do that; Java is not C++ and C++ is not Java.

    kind regards,

    Jos

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by Mark Robert View Post
    Hi, how can I create default arguments in java like in C++?
    e.g., void MethodName( int a = 1, int b = 1 );
    This way I can call that function with/without specifying arguments.
    You can't, a normal hack is overloading the method:
    Java Code:
    void MethodName( int a, int b ){}
    void MethodName(){
      MethodName(1,1);
    }
    Quote Originally Posted by Mark Robert View Post
    One more thing, how can I create functions outside a class? Is that possible? Please help..
    Nope.

    EDIT: What Jos said :P
    Last edited by PhHein; 08-17-2010 at 02:39 PM. Reason: Being slooooow
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhHein View Post
    EDIT: What Jos said :P
    Man, I'm fast today ...

    kind regards,

    Jos (<--- speedy ;-)

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You are, my friend :)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Mark Robert is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks for the quick response guys.. now I know. How about macros? Can I create macros in Java? If yes, how?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Mark Robert View Post
    thanks for the quick response guys.. now I know. How about macros? Can I create macros in Java? If yes, how?
    Nope, or you have to use an external macro processor.

    kind regards,

    Jos

Similar Threads

  1. Copy Default table model to another default table model?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2010, 04:08 PM
  2. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-07-2010, 11:30 PM
  3. Replies: 1
    Last Post: 03-10-2009, 01:12 PM
  4. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 09:37 PM
  5. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •