Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pass array as literal to method

Posted on 2007-04-03
3
Medium Priority
?
200 Views
Last Modified: 2013-11-23
Hi,
I have a constructor which accepts an array as an argument.
I want to create a new instance of the object and pass it some literal values for the array, but I cant seem to find the correct syntax for this. Can anyone help?
As an example:
private static int [] DOB = new int[2];
public Object(String name, int [] DOB)
       {
      etc..
       }

Object person= new Object("Bart", {12,05});
So this would create a new object whose name is bart and date of birth 12th of May.
Unfortunately this wouldnt compile as I could not pass the array like this.

Cheers
John
0
Comment
Question by:jdav3579
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 375 total points
ID: 18845171
The syntax you propose is reasonable; unfortunately it is not supported. You'll have to do the obvious instead:

int [] dob={12, 5 };
Object person=new Object("Bart",dob);

Or, of course, you could create an alternate constructor that takes two ints instead:

public Object(String, name,int day,int month)
{    ...
}

then you could call it with:

Object person=new Object("Bart",12,5);
0
 
LVL 16

Expert Comment

by:imladris
ID: 18851271
Did that help?

If so it is now time to grade the answer.

If not, perhaps a clarifying question would help.
0
 

Author Comment

by:jdav3579
ID: 18852559
Hi imladris,
I have only just had a chance to try it - but yes it did help thanks.
John
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question