• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

What is wrong with my code?  RSS feed

 
Ranch Hand
Posts: 50
Fedora Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
weightList[0]= s; is a statement. All java statements must be in a method or constructor or code block. You cannot just add a statement to a class. Adding braces around the weightList[0]= s; statement (putting it in an initializer code block) fixes the problem. You could also initialize weightList[0] in a constructor.
 
Venkata Raaman
Ranch Hand
Posts: 50
Fedora Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Reilly wrote:weightList[0]= s; is a statement. All java statements must be in a method or constructor or code block. You cannot just add a statement to a class. Adding braces around the weightList[0]= s; statement (putting it in an initializer code block) fixes the problem. You could also initialize weightList[0] in a constructor.


Thanks I got it...and some more questions here...

and I can also initialize and assign the above code inside constructor with no errors. Can I do that way, which is best practice
 
Venkata Raaman
Ranch Hand
Posts: 50
Fedora Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Reilly wrote:weightList[0]= s; is a statement. All java statements must be in a method or constructor or code block. You cannot just add a statement to a class. Adding braces around the weightList[0]= s; statement (putting it in an initializer code block) fixes the problem. You could also initialize weightList[0] in a constructor.


Sorry I got it except my last question...!!
I can Also do Car myCars3 [] = new Car[3]; inside constructor which is best practice to do?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!