Solved

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

Posted on 2009-07-09
3
256 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
[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 60

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 60

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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