• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Syntax error on token <variableName>, VariableDeclaratorId expected after this token

 
Avanti Deshpande
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the Code :-

This program runs perfectly fine. But if I remove curly braces from the statement {s = "C++";} from XClass or from {s = "C#";} from YClass. It gives me an compilation error that "Syntax error on token s , VariableDeclaratorId expected after this token".
1. Can any one please tell me what is the importance of curly braces in these statement ?
2. If I run YClass it gives me the value of super.s as "C#". My question is why not "C++". Since in XClass the reinitialization happened for this veriable. What I need to do to get value as "C++" ?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. It is called an initialization block. It runs when an Object of that class is created.
2. The initialization block in YClass sets the value of s to "C#". Therefore the old value cannot be retrieved.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the JavaRanch!
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. {} is required cause you are assigning value. You are not declaring it.
2. As you have intiated the object using XInterface ob = new YClass(); this statement. JVM will execute curley braces and assign value to it as C#. hence answer is C#.

Regards,
Rushi
 
Avanti Deshpande
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter and Rushikesh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic