• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this(a)

 
Ideal Me
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very confused by the compilation error: can't reference a before the supercalss constructor has been called.
please read the following code and I look forward to explaination.
class Test {
int y=1;
int a=6;
Test() {
this(a); /* Complilatrion error: can't reference a before the supercalss constructor has been called. However, when I added the "static" modifier before the declarion "int i=6;", the program works ok. */
}
Test(int x) {
if ( x > y )
a = y*6;
else
a = y*9;
a++;
System.out.println(a);
}
public static void main(String [] args) {
Test t = new Test();
}
}
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read
Test Your Java Knowledge, Question 12 for very good detailed explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic