• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with code

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic