?
Solved

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

Posted on 2009-07-09
3
Medium Priority
?
258 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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