• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass predefined size array as argument  RSS feed

 
Primrose Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this possible in Java?



What I am trying to do is to get the person who is using the method to send a year in the format 1998 and so on.

What would be the best way to accomplish this?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't specify the size of the array in the method declaration.

I'd just pass in an int (or possibly a String depending on what was more appropriate in your application) and then check the value was in the appropriate format/range and if not throw an exception.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primrose Smith wrote:What I am trying to do is to get the person who is using the method to send a year in the format 1998 and so on.

How is specifying that the argument should be an array of 4 integers going to help with that? I could pass in for example { 23, 55, 282, 128938 }. That's an array containing four integers.

There's no way to specify that the argument must always have four digits, and since the value you pass in is not known at compile-time, there's also no way to check for the compiler if the user of your code is calling it correctly.

You can put a runtime check inside your method:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!