• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!