Forums Register Login

Problem with code

+Pie Number of slices to send: Send
package ocean;

public class Fish {
public Fish() { }
protected int size=11;
protected void swim() {
System.out.println("Swimming in ocean");
}
}

----------------------------------------------------------------------------------

package river;
import ocean.Fish;

class Tuna extends ocean.Fish {

static int i=size++;//accesses inherited var.

//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


public static void main(String args[]) {

System.out.println(i);
//Why do we get error here even if we have declared
//the variable 'i' to be static ???

Tuna t=new Tuna();
t.size=13; //is this allowed

//The following is certainly not allowed
//CERTAINLY NOT ALLOWED
Fish f=new Fish();
f.size=14;
}
}
+Pie Number of slices to send: Send
//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 ]
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Originally posted by marc weber:

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.


Why? i is just as static as the main method.
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 869 times.
Similar Threads
Unexpected result
Kindly explain this code from SCJP faq
Interfaces
protected doubt
loop problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:08:29.