Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java classes accessiblity

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using jcreator

i made 2 classes in java
test.java
UseMeTest.java
saved both in the same folder

--------Test.java---------
public class Test
{
public static void main(String arg[])
{
UseMeTest ob=new UseMeTest();
System.out.println(ob.a);
}
}



-------UseMeTest.java----------


class UseMeTest
{
int a=10;
}



both the java are compling fine
and the output of Test.java is
10


i think this is wrong . i mean to say is that how could a class access other class object as i did not put them in same package neither the UseMeTest.java class is declared as public
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mentioned that you placed both classes in the same directory - that means they share the same package.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both the classes are in the same folder but i have not used any package statement in my code.
is there some default package created ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. If you don't put them in a package, they'll be in the so-called default package. You should avoid that, and always put classes in packages.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the next time you post some code, please UseCodeTags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic