• Post Reply Bookmark Topic Watch Topic
  • New Topic

This code should work but it doesn't! Help!  RSS feed

 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//So here is the code:
//****************************************

class Test {

public void dataX () {
int x = 100;
}

public static void main (String[] args) {
Test intX = new Test();
System.out.println (intX.dataX.x);
}

}

//****************************************

Please help,I don't understand why this code won't compile.
It should print out 100 to the console but instead gives and error.

symbol : variable dataX
location: class Test
System.out.println (intX.dataX.x);
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to be careful when you call the members of a class definition. If you only use a name, then the compiler is looking for a variable. If you include parentheses with optional parameters, then the compiler thinks it is a method.
 
pradeep selvaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot access the variable x as you have done. Try this instead.
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response. Now I can finally go to sleep.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!