java - Is it possible to create multiple classes in one package and import them in another package? -


hi have question on java package. have created package more 1 classes [package name 'animal' , file name 'zoo.java']. have created java file in package 'mypackage' , created file called 'main.java'. tried accessing members of 'animal' package 'main.java' , throwing me error because cannot access classes of 'animal' package. know classes in same package should accessible each other, or should make public. can change 1 class public in 'zoo.java'. found 1 solution create separate java files each class in animal package , make public. there other method ? understanding was, can write classes in 1 file , import them anywhere. below screenshot. enter image description here

and 1 more thing, in java library how implemented it? separate source files each class within package ?

so found 1 solution create separate java files each class in animal package , make public.

if want use classes outside of package they're defined in, yes, have public. , public parts of them accessible, except subclasses, have access protected parts of them. more in tutorial.

and yes, there can 1 public class in given file (and file's name must match public class's name). in java, tend toward doing anyway. helps avoid files getting huge , difficult work in.

is there other method ? understanding was, can write classes in 1 file , import them anywhere.

i'm not sure have got understanding from. use classes outside packages, have public, , public have in separate files.

from comment on answer:

...in case if made elephant public in zoo.java can access particular class outside not others fish , bird.

right. if want them public, rather having zoo.java, have elephant.java, fish.java, , bird.java.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -