Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Array Doubts

 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have some doubts with anonymous array creation. Here is my code:


If I don't put that code in a main method it gives the following error:
AnonymousArray.java:9: <identifier> expected
testScores = new int[] {4,7,2};

Why is this?

And also, this code works outside of a main method:
int[] testScores = {4,7,2};
And so does this:
int[] testScores = new int[] {4,7,2};

So why doesn't that particular code work?



 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your other two examples are valid declaration and initialization statements.
Your two lines of code must be placed within a method() or an initialization
block like this:
{
int[] test;
test = new int[] {1,2,3,4,};
}

Jim ... ...
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Hoglund wrote:Your other two examples are valid declaration and initialization statements.
Your two lines of code must be placed within a method() or an initialization
block like this:
{
int[] test;
test = new int[] {1,2,3,4,};
}

Jim ... ...

Thanks for the reply, but yes, I already know that. I'm asking why it's like that?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Vella Zarb wrote:
...I'm asking why it's like that?


Best answer I can come up with: Because that's how it's defined in the JLS.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

Some answers and some questions for you to research (because after all, you ARE studying for the SCJP ):

- first off, Java allows a shortcut syntax to declare and construct an array in one stmt. - as you've discovered. Who knows why they allow that shortcut, but it's nice and it's used all the time.

Okay, now some questions for you:

- you said 'main method'... did you try non-main methods? I'm not trying to nitpick, but it's important for you to understand when there ARE distinctions between main() and other methods, and when there aren't. (Also, it's important to understand the distinctions between static and non-static methods.)

- another question for you - in the 2 lines of code you apparently tried to compile outside of main(), can you describe those 2 lines of code? Hopefully, once you have a way to describe that code it will help you answer your own question.

In a nutshell, when you're studying this stuff I urge you to get really clear and precise on the terms you use. As you go along, it will be impossible to discuss and understand the harder stuff without a really solid understanding of the basic stuff. For those of you reading along, I'll give you an example... one reason that GC is included in the exam is that it forces you to REALLY understand how objects and object references relate to each other. In our experience it's shocking how many Java programmers are a little vague on those topics. So, think of the SCJP as a way to demonstrate that you're rock solid on the fundamentals, and remember that being rock solid on the fundamentals isn't at all trivial!

Does that make sense?

Bert
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waiting for reply.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Waiting for reply.

Reply to what??

@Mike, inside the class, you can have only variables and method declarations, other classes (inner classes) and initialization blocks. If you try to do this
You'll get a compilation error at the 2nd line. What is the JVM supposed to do with that line?? It is a normal statement like a System.out.println or anything else. To execute a general statement at the time of class loading or instance creation, you use static and instance initializer blocks respectively. That way the JVM knows what to do with that statement and when to execute it...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Abimaran Kugathasan wrote:Waiting for reply.

Reply to what??

@Mike, inside the class, you can have only variables and method declarations, other classes (inner classes) and initialization blocks. If you try to do this
You'll get a compilation error at the 2nd line. What is the JVM supposed to do with that line?? It is a normal statement like a System.out.println or anything else. To execute a general statement at the time of class loading or instance creation, you use static and instance initializer blocks respectively. That way the JVM knows what to do with that statement and when to execute it...


Thanks Ankit,

I also have this problem, and there is no way to get notified for the posts, that's why, I use this way.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran you can watch a topic that you are interested in. There is this button near post reply etc buttons which you can use to add a topic to your watch list...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Abimaran you can watch a topic that you are interested in. There is this button near post reply etc buttons which you can use to add a topic to your watch list...


Thanks a lot for your valuable information!
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your replies. I seem to have figured it out now. The code that wasn't working had to be in a method, constructor, or initialization block so that it could be ran.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic