Solved

pass array as literal to method

Posted on 2007-04-03
3
191 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 38
MySQL  on Tomcat 8 43
Unhandled exception type Exception 18 28
Android development question 2 34
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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

785 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