This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Simple issue of public variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple issue of public variable" Watch "Simple issue of public variable" New topic
Author

Simple issue of public variable

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Getting compile time error saying variable xx not found. Please help !

class th3
{
public int xx = 10;
public static void main(String s[])
{
proc p = new proc();
p.padd();
}
}

class proc
{
void padd()
{
System.out.println(xx);
}
}


"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Hi,

How are you tring to find the variable xx of class th3 in class proc?

You should have instance of class th3 or some other way to access the variable.
I would suggest you to read chapetr on scopes and varibles.

Regards
Sandeep Jindal


SCJP 5.0
http://sites.google.com/site/duddlutechnologies/home
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
hi nitin

in your code you have two different classes, which are not related to each other in any way. if you want the variable xx to be accesed in other class you can either write :

"class <first class> extends <second class>

so that there is a relation established in them.
Otherwise the only option will be to create an instance of the other class and then access that variable through this object/instance.

bye


Impossible is I M Possible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple issue of public variable
 
Similar Threads
Initializers...
doubt about the program???
static block
I think D is right, but someone told me C is right.
Clear Forms