• Post Reply Bookmark Topic Watch Topic
  • New Topic

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!