Solved

Java array with direct adressing (={...})

Posted on 2009-07-09
3
251 Views
Last Modified: 2012-05-07
Hi there
Did someone know why the first code doesn't work?
public class DealProposition {

    public Deal[] dealPropList;

    public DealProposition(Deal d1, Deal d2) {

        dealPropList = {d1, d2};//Doesn't work???? "Illegal start of expression"

    }

}
 
 

public class DealProposition {

    public DealProposition(Deal d1, Deal d2) {

        Deal[] dealPropList = {d1, d2};//Works

    }

}

Open in new window

0
Comment
Question by:Nargzul
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24813420
Believe it is because in the second being a declaration of the array, it understands better the {d1, d2} syntax.  For the first, you could just let system know this is array creation by adding new Deal[] in front.
public class DealProposition {

    public Deal[] dealPropList;

    public DealProposition(Deal d1, Deal d2) {

        dealPropList = new Deal[]{d1, d2}; //this should work

    }

}

 

Open in new window

0
 
LVL 1

Author Comment

by:Nargzul
ID: 24813510
Yes it works, I don't really  understand why, but ok
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24813603
Sorry I didn't do a good job of explaining.  The way I showed is the proper syntax.  Remembering you can use the literal array initialization anonymously, so needs to know the type.

public void setArray(String[] array) { }
setArray(new String[]{"1", "2", "3",});

The shorthand {"1", "2", "3",} without the type and new keyword is legal if used in line with variable (array) declaration as a valid initializer.  This is why it worked in your second case, but not in the first.

Best regars,
Kevin
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
Need to track down Infection in a Server 2008 domain user profile 7 48
diffSum example 4 21
oracle 11g 23 47
table example 4 20
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

919 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

14 Experts available now in Live!

Get 1:1 Help Now