aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Array Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Array Doubts" Watch "Anonymous Array Doubts" New topic
Author

Anonymous Array Doubts

Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
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?




It is a mistake to think that you can solve any major problem with just potatoes.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 14, 2002
Posts: 8815
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Waiting for reply.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Dec 28, 2009
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Array Doubts