Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String not sure where I am going wrong in a simple code snippet

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Please adivce where I am going wrong

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:Hi Please adivce where I am going wrong


Where you're going wrong is 1) Not telling us clearly and precisely what you're trying to do and 2) Not telling us clearly and precisely exactly what problems you're encountering.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting compilation error


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All statements must be inside a method, constructor, or initializer block. The only things that can occur outside those contexts are member variable definitions and definition+initialization combinations.

The line

does not fit those criteria, so it has to be inside a method, constructor, or initialization block.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal Hegde,


you can declare and intialize the instance variables but you cannot do assignments.

class A
{
String s="oh my god";
s="aaa";//this is error
}


check line number 9 in your program


Regards,
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic