| Author |
accesing variable of default package inside the package class
|
R.Joe
Ranch Hand
Joined: Jun 16, 2004
Posts: 31
|
|
Pl help me out to run this program or how to access variable y inside the X1 class of a1 package
default package class
/////////////////////
import a1.*;
class AA
{
public int y=2;
public static void main(String[] args)
{
X1 xi =new X1();
xi.show();
}
}
///////////////////////
package class is
///////////////////////////////////
package a1;
public class X1
{
public int i = 0;
public void show()
{
System.out.println("value is "+i);
System.out.println("value is "+y);
}
}
////////////////////////
regards
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8435
|
|
Pl help me out to run this program or how to access variable y inside the X1 class of a1 package
There is no variable 'y' in X1 class. It exists in AA class.
If you want to access 'y' from X1 in the show method, you can do it by AA.y. For this to work you will have to declare 'y' as public static though.
"R.Joe",
Please choose your forums carefully.
Nothing UI related here.
Moving.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
R.Joe
Ranch Hand
Joined: Jun 16, 2004
Posts: 31
|
|
sorry for wrong posting
after declaring variable y as public static and accessing that variable inside X1 class as per your suggestion "AA.y", it will not works , it gives compilation error "can't access AA class inside X1 class
pl help
regards
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
You can't get access to classes inside the default package from classes in named packages. The compiler has been told not to compile it.
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8435
|
|
Oh ok.
I forgot all about that restriction (since 1.4 I think). Thanks Campbell.
Just move your AA class to some package to get it working.
|
 |
 |
|
|
subject: accesing variable of default package inside the package class
|
|
|