//why does the compiler says "identifier expected" when //we say size++; instead of the above code line ??? //A statment like System.out.println(size++); also doesn't //work if written in plae of above code line
//why does the compiler says "identifier expected" when //we say size++; instead of the above code line ??? //A statment like System.out.println(size++); also doesn't //work if written in plae of above code line
Because this is not within a method. You can declare (and initialize) a variable here (or any other class member), but you can't perform an "action" (like incrementing a variable or calling a method).
//Why do we get error here even if we have declared //the variable 'i' to be static ???
Because size is not declared static in Fish, so you're trying to access a non-static variable (size) from a static context (main).
//The following is certainly not allowed //CERTAINLY NOT ALLOWED Fish f=new Fish(); f.size=14;
It works for me. Why would this be a problem? [ October 31, 2005: Message edited by: marc weber ]
Originally posted by marc weber: //why does the compiler says "identifier expected" when //we say size++; instead of the above code line ??? //A statment like System.out.println(size++); also doesn't //work if written in plae of above code line
Because this is not within a method. You can declare (and initialize) a variable here (or any other class member), but you can't perform an "action" (like incrementing a variable or calling a method).
Although I haven't tested it, I think you CAN perform actions in static declaration with immediate initialization. The problem is, i is static but size is not. Hence it does not recognize size.
//Why do we get error here even if we have declared //the variable 'i' to be static ???
Because size is not declared static in Fish, so you're trying to access a non-static variable (size) from a static context (main).
Fish has nothing to do with this - both the main method and i variable are static inside Tuna. The problem is, because of the previous error, i is not correctly been added to the list of known variables and therefore is not recognized.
Edit: put it in a text editor, tried to compile, and got the following error: Once I make size in Fish static it's ok and compiles (Java 1.4.2). [ October 31, 2005: Message edited by: Rob Spoor ]
Originally posted by Rob Spoor: I think you CAN perform actions in static declaration with immediate initialization.
Yes, you certainly can. But I believe the original question was why you cannot perform an operation (++) or call a method (System.out.println...) without a declaration.
Originally posted by Rob Spoor: The problem is, because of the previous error, i is not correctly been added to the list of known variables and therefore is not recognized.
Yes, this is definitely part of the problem. But I was assuming that the above error would be corrected, in which case there's still a "non-static variable from static context" issue.
Yes, this is definitely part of the problem. But I was assuming that the above error would be corrected, in which case there's still a "non-static variable from static context" issue.
Note that the first question in the orignal post was why we can't simply have the following line in a class...
size++;
The reason is that an isolated operation like this needs to be within a method. However, you can perform an operation like this as part of initializing a variable at the point of declaration...
static int i = size++;
So the above line corrects that particular issue by performing the operation as part of initialization, and this is what I meant by assuming that the above error would be corrected. (In fact, it is corrected in the posted code.) And because i is static, it can certainly be accessed from the println in main. But the separate issue is that the static variable i is attempting to access a non-static variable size.
I think the confusion is that I assumed that an error was also being generated by the println in main, as reported by the original poster. However, in checking this, I cannot reproduce such an error.
Post by:autobot
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards