Solved

pass array as literal to method

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 57
How to log java errors in tomcat 8 39
activeMQ Queue Messages Enqueued and Messages Dequeued 1 119
How to fix  socket closed error 11 64
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…
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 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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 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