• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why This Compiler Error?

 
Gary Farms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile this program, I get a compile error that says, "No variable i defined in class Test". Why? Variable i does exist in class Test! I simply want to instantiate an object t and store an integer value in the i field of object t.
Here's the small program:
class Test
{
public static void main(String[] args)
{
int i;
Test t = new Test();
t.i=9;
}
}
 
deep venu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats cause i is not a instance member of test class
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sjsearcher:
When I compile this program, I get a compile error that says, "No variable i defined in class Test". Why? Variable i does exist in class Test! I simply want to instantiate an object t and store an integer value in the i field of object t.
Here's the small program:
class Test
{
public static void main(String[] args)
{
int i;
Test t = new Test();
t.i=9;
}
}

The person who posted before me is correct. You declared variable "i" locally, but you referenced it as if it were a meber of "t".
The following will work...

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sjsearcher,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic