• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Splitting up the declaration and initialisation of a ArrayList type not working.

 
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I'm don't know why the code I've commented above does not work whilst the code below it does. They both do the same thing.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if this is the line you are referring to the compiler is probably unaware of what an ArrayListList is - I guess that's supposed to be ArrayList.
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay lets try that again...

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java statements, that are not declarations, must be in a constructor, initializer, or method. Java doesn't just allow you to place statements anywhere.

Henry
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Java statements, that are not declarations, must be in a constructor, initializer, or method. Java doesn't just allow you to place statements anywhere.

Henry



 So why does this work....

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fergus Flanagan wrote:

Henry Wong wrote:
Java statements, that are not declarations, must be in a constructor, initializer, or method. Java doesn't just allow you to place statements anywhere.



 So why does this work....



That compiles because that line is a declaration. Please read my response again.

Henry
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Fergus Flanagan wrote:

Henry Wong wrote:
Java statements, that are not declarations, must be in a constructor, initializer, or method. Java doesn't just allow you to place statements anywhere.



 So why does this work....



That compiles because that line is a declaration. Please read my response again.

Henry



Henry I did read your response! So please bear with me while I explain why I questioned it.

You see I've always thought a declaration was


And instantiating as..
 // so this can also be assumed to be a declaration even though it's declaring and instantiating 'x'
or


I think that was a fair assumption to make on the part of a beginner.


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 3 is only an initialization, and that's illegal outside of a method, constructor, or initializer.  A declaration with an initialization is okay.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As an FYI, my last response this morning, was very terse, because I was literally shutting down my computer, on the way out the door, in order to pick up a few relatives, and to do a bunch load of errands... Anyway, I just got back, and noticed that I offended the OP.

Apologies for any offense, it was really not what I intended...

Henry



 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
As an FYI, my last response this morning, was very terse, because I was literally shutting down my computer, on the way out the door, in order to pick up a few relatives, and to do a bunch load of errands... Anyway, I just got back, and noticed that I offended the OP.

Apologies for any offense, it was really not what I intended...

Henry





Okay Henry thanks for offering your wisdom in the first place.

Crying myself to sleep last night helped (only kidding)    

I knew this but for some reason it became unknown.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fergus Flanagan wrote:. . .
You see I've always thought a declaration was


And instantiating as..
 . . .
I think that was a fair assumption to make on the part of a beginner.

At the risk of looking like an anally‑retentive pedant, the second piece of code is declaration and initialisation together (as Knute has already said). Instantiation means something different. You need to be careful and fussy and pedantic with such jargon; if you use the jargon right all other Java® programmers will know immediately or sooner exactly what you mean.
And you s‍hould never assume anything, because every now and again people assume the wrong thing and that starts a long series of disasters.
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Fergus Flanagan wrote:. . .
You see I've always thought a declaration was


And instantiating as..
 . . .
I think that was a fair assumption to make on the part of a beginner.

At the risk of looking like an anally‑retentive pedant, the second piece of code is declaration and initialisation together (as Knute has already said). Instantiation means something different. You need to be careful and fussy and pedantic with such jargon; if you use the jargon right all other Java® programmers will know immediately or sooner exactly what you mean.
And you s‍hould never assume anything, because every now and again people assume the wrong thing and that starts a long series of disasters.



Yes I realised this after posting. Instantiation without looking it up is used to describe what happens to objects I believe.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to add the pedantry going on in this thread, it is better to declare your variables with the Interface type rather than the Concrete type.

i.e.


If you suddenly decide you need it to be a LinkedList rather than an ArrayList you can then just change it in one place, and the rest of your code will still work.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fergus Flanagan wrote:. . . . Instantiation without looking it up is used to describe what happens to objects I believe.

Instantiation means creating an object from the class, which can also be called instantiating the class. That object is called an instance of the class.
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Fergus Flanagan wrote:. . . . Instantiation without looking it up is used to describe what happens to objects I believe.

Instantiation means creating an object from the class, which can also be called instantiating the class. That object is called an instance of the class.



Yes just been learning about this on the edx course I'm doing
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Fergus Flanagan wrote:. . . . Instantiation without looking it up is used to describe what happens to objects I believe.

Instantiation means creating an object from the class, which can also be called instantiating the class. That object is called an instance of the class.



Yes you've worded it better than me
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the great responses guys/gals.

Of all the forums I've been using over the years for whatever you care to mention this comes out tops for responsiveness, expertise and helpfulness.

Hope I can get to the point where I'll be able to repay the help I've been given and maintain the professionalism shown here
reply
    Bookmark Topic Watch Topic
  • New Topic