• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

accesing variable of default package inside the package class

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76100
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic