• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

how to access instance variables

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am new to java.Please help me in solving this problem.
I have written a small program proably took the help from the book Head first java.

Its giving me compilation error when i am trying to access variable GNum ie at lines Player1Num =Player1.GNum;,Player2Num =Player2.GNum; and Player3Num =Player3.GNum;.
Error coming is "Cannot find sysmbol variable GNum".How do I access the variable of a method defined in a class from another class.

This is the same way this code is written in head first java.

If any one has the answer please reply.
class GuessGame
{
Player Player1 = new Player();
Player Player2 = new Player();
Player Player3 = new Player();

int Player1Num=0;
int Player2Num=0;
int Player3Num=0;

boolean P1IsRight =false;
boolean P2IsRight =false;
boolean P3IsRight =false;

public void StartGame()
{

int targetNumber = (int) (Math.random() * 10);
System.out.println("Let s start the game of gussing the numbers");

while(true)
{
System.out.println("Its the turn of Player1. Please guess a number between 0 and 9");

Player1.Guess();
Player1Num =Player1.GNum;

System.out.println("Its the turn of Player2. Please guess a number between 0 and 9");

Player2.Guess();
Player2Num =Player2.GNum;

System.out.println("Its the turn of Player3. Please guess a number between 0 and 9");

Player3.Guess();
Player3Num =Player3.GNum;

if(Player1Num == targetNumber)
{
P1IsRight = true;
}

if(Player2Num == targetNumber)
{
P2IsRight = true;
}

if(Player3Num == targetNumber)
{
P3IsRight = true;
}

if (P1IsRight || P2IsRight || P3IsRight)
{
System.out.println("We have winner");
System.out.println("Is Player1 a winner?" + P1IsRight);
System.out.println("Is Player2 a winner?" + P2IsRight);
System.out.println("Is Player3 a winner?" + P3IsRight);
break;
}
else
{
System.out.println("Lets play the game again as we dont have any winner");
}
}
}
}

class Player
{

int GuessNum;

public void Guess()
{
int GNum = (int) (Math.random() * 10);
System.out.println("I am guessing the number as" +GNum);
}
}

public class GameLauncher
{

public static void main(String[] args)
{

GuessGame Game = new GuessGame();
Game.StartGame();
}
}
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GNum is locale variable inside method.
Please make it instance variable inside the class and mark it as public.

V
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.It worked
 
Marshal
Posts: 78653
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vishal Matere:
GNum is locale variable inside method.
Please make it instance variable inside the class and mark it as public.

V

Public?



Avoid making fields public. You are right to make gNum (please spell it like that) a field, but give it private access and set up a getGNum() method to allow access to it.
 
Campbell Ritchie
Marshal
Posts: 78653
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like thisand in the Player class. . . and avoid while(true) . . . break; Change it to
while(!(P1isRight || P2isRight || P3isRight)) . . . and lose the break;
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic