aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with code " Watch "Problem with code " New topic
Author

Problem with code

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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;
}
}


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with code