Solved

pass array as literal to method

Posted on 2007-04-03
3
190 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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 72
JAVA part two 5 58
ArrayIndexOutOfBoundException 9 68
JUnit 4 @Before and @BeforeClass differences 3 49
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now