Solved

pass array as literal to method

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

708 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

11 Experts available now in Live!

Get 1:1 Help Now