I would like to know how the default package is defined in Java.I know how public and private access is defined but I don't know whether there is any default package access that is defined in package level access in java.

I'm asking this question because when I compile java file containing two classes since no modifier is given for classes,java compiler would give package level access as the default access modifier for the classes.Since no package is defined,java compiler would use the default package but I couldn't get whether the default package is included in package level access in java.Could anyone help me.

  • 1
    what is in tutorial that you don't understand? Java tutorials > Learning the Java Language > Packages: "For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the java.lang package, and (3) the current package (the package for the current file)..."
    – gnat
    Commented Dec 3, 2014 at 14:03
  • @gnat:I would like to know whether access and use mean the same in context of programming.I'm having this doubt since I have two individual classes in a single java file and even after changing both classes to public I can't access one variable in another class.But as said in:docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html ,it states that public access has Y for world that means any class can access an public class members isn't it?
    – justin
    Commented Dec 3, 2014 at 14:10
  • what you describe sounds like nested classes (Java tutorials > Learning the Java Language > Nested Classes). Although without seeing your code it's quite unclear what you're asking
    – gnat
    Commented Dec 3, 2014 at 14:16
  • @gnat:I'm not asking about nested classes but I'm asking about a class followed by another.The code I'm asking is available on this post:programmers.stackexchange.com/questions/264423/…. Could you help me in the previous comment.
    – justin
    Commented Dec 3, 2014 at 14:21

1 Answer 1


The default package is the package without a name, all classes without a package declaration at the top of the file fall into it.

It is subject to all normal rules of packages except that you can't reference classes of it from a class inside a package.

For example I have 2 java files:

public class A{
    public static void foo(){System.out.println("fooCalled");}


package com.example;

public class B{
    public static void main(String[] arg){
        A.foo();//won't compile


Then B (or in the qualified form com.example.B) can never call the foo of A without reflection magic.

If you ever start making larger programs then you should put all your code in packages. It allows you to segregate your code in a way that makes sense.

The accepted practice is to name you package after a domain the program is part of like com.example

  • :Could you tell me what do you meant by "at the top of the file".Also I couldn't get what you meant by "you can't reference classes of it".By the second statement do you mean that we can't refer classes each other in the same package?That means if you have package A and have two classes B and C do you mean that you can't refer the method of B in A or vice versa?
    – justin
    Commented Dec 3, 2014 at 13:45
  • :I have updated the post.Could you look at the post and tell me whether default package is included in the package level access.
    – justin
    Commented Dec 16, 2014 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.