aspose file tools*
The moose likes Beginning Java and the fly likes Why This Compiler Error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why This Compiler Error?" Watch "Why This Compiler Error?" New topic
Author

Why This Compiler Error?

Gary Farms
Greenhorn

Joined: Jul 31, 2002
Posts: 12
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

Joined: Jun 09, 2001
Posts: 56
thats cause i is not a instance member of test class
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
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...



Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
jQuery in Action, 2nd edition
 
subject: Why This Compiler Error?