• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

What is wrong with my code?

 
Ranch Hand
Posts: 50
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic