Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
count7 challenge 12 69
HashMap Vs TreeMap 12 48
Java - Why doesn't this JFrame work 3 16
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
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…

707 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

13 Experts available now in Live!

Get 1:1 Help Now