• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • 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.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Okay lets try that again...

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.


 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!