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
Solved

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

Posted on 2009-07-09
3
253 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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
Running JavaFX on JDeveloper 12C 1 53
Android development question 2 53
How do I remove an object from a 3 40
Java Restore security prompts not working 10 56
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…
This video teaches viewers about errors in exception handling.

840 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