1

Hi I have a question on Java Package. I have created a package with more than one classes [package name is 'animal' and file name is 'Zoo.java']. I have created another java file which is in another package 'mypackage' and created a file called 'MAIN.java'. I tried accessing members of the 'animal' package from 'MAIN.java' and it is throwing me an error because it cannot access classes of 'animal' package. I know that only classes which are in same package should be accessible each other, or it should make as public. But I can only change one class to public in 'Zoo.java'. So I found one solution that create separate java files for each class in animal package and make it as public. Is there any other method ? My understanding was, we can write all classes in one file and import them from anywhere. Below is my screenshot. enter image description here

And one more thing, in java library how they are implemented it? separate source files for each class within a package ?

Sarath S Nair
  • 603
  • 2
  • 14
  • 29
  • Please check https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – SacJn Sep 21 '15 at 05:26
  • @SacJn OP is talking about packages, not members in class :) – Suresh Atta Sep 21 '15 at 05:29
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ Reason of putting this question was that OP was not able to access variables of the class in different package. So I put this link to make him aware that even if OP has classes in two different packages, members can be accessed. And from this question it was quite evident that OP has no idea of access modifiers. – SacJn Sep 21 '15 at 05:33

4 Answers4

5

So I found one solution that create separate java files for each class in animal package and make it as public.

If you want to use classes outside of the package they're defined in, yes, they have to be public. And only the public parts of them are accessible, except to subclasses, which also have access to the protected parts of them. More in this tutorial.

And yes, there can be only one public class in a given file (and the file's name must match the public class's name). In Java, we tend toward doing that anyway. It helps avoid files getting huge and difficult to work in.

Is there any other method ? My understanding was, we can write all classes in one file and import them from anywhere.

I'm not sure where you would have got that understanding from. To use classes outside their packages, they have to be public, and to be public they have to be in separate files.

From your comment on another answer:

...in my case if i made Elephant as public in Zoo.java then I can only access that particular class outside not others like Fish and Bird.

Right. If you want them all to be public, rather than having Zoo.java, have Elephant.java, Fish.java, and Bird.java.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You have another way to solve your problem. You can use inner class as follow.

 package animals;

 public class Elephant {

 public static class Fish{
    public void print(){
      System.out.println("test");
   }
 }
}

and in main package:

  package main;

  import animals.Elephant;


  public class Main {
   public static void main(String[] args) {
     Elephant.Fish fish = new Elephant.Fish();
      fish.print();
   }
  }

But it is not recommended design. If design is necessary define seperate java file and mark as public. it is the best way.

Vaseph
  • 704
  • 1
  • 8
  • 20
0

There can be multiple classes inside one JAVA file provided that only a single Class is made Public.

See this Multiple classes in single file

Community
  • 1
  • 1
0

In this case you can use inner classes. See the following example. Here the inner class is accessible from classes in other packages.

    public class Outer {        
      public class Inner {
      }
    }
thanuja
  • 546
  • 1
  • 8
  • 22