• Post Reply Bookmark Topic Watch Topic
  • New Topic

compilation error  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI there,
I tried running this program but shows some compilation error of cannot find symbol . in following:
p1.num, p2.num and p3.num




please could someone help???
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,
In the future, please post the actual error message. It helps when looking what is wrong.

In your clase, the problem is that these lines expect there to be an instance variable named num in the Player class:


However, there is a local variable declared inside the method and no instance variables.

Also, a tip: In Java, there is a convention to begin variable names with a lower case letter. This convention makes it easier to determine what is a variable (vs a class) quickly.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below line of code



You are basically trying to access num which resides inside P1,P2,P3.

If your code is not able to access a member in class it means either that member is not defined in your class or the member has restrictive access.
See you Player class and try to find out is there "num" defined . You will understand the reason of this error.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. So because these are supposed to be declared as instance variables.
what about randomnum??? i didnot declare this separately but there is no error??
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
randomNum is only used in the method it is declared. So a local variable is appropriate there.
 
Zulfiquiar Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Gurung wrote:OK. So because these are supposed to be declared as instance variables.
what about randomnum??? i didnot declare this separately but there is no error??


Two reasons

1> Because randomNum is defined as local variable inside the method and it is accessed inside the same method.

2>Because you are not accessing randomNum using any reference variable. If you had it would have failed and given you the same error as you got for num.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!