• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accesing variable of default package inside the package class

 
R.Joe
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R.Joe
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic