-2

see the classes hirachy below, I was confused very much that why two isolated classes can exist in one java file.

I know the two classes have something to do with each other, but the problem is I do not clearly understand what exactly realationship between them.

Also, if you noticed that NewFileTask extends TaskDialog.Task not TaskDialog, I can not understand what does that mean, and what does that used for? anyone can help? Any material or reference for me to understand will be appreciated. Thanks in advance!

class NewFileTask extends TaskDialog.Task {

    public NewFileTask(String repoID, String parentDir,
                       String fileName, DataManager dataManager) {

        @Override
        protected void runTask() {
        }
    }
}

and next is the class exits in the same java file

public class NewFileDialog extends TaskDialog {

    public void init(String repoID, String parentDir, Account account) {

    }

    @Override
    protected View createDialogContentView(LayoutInflater inflater, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_new_file, null);
        fileNameText = (EditText) view.findViewById(R.id.new_file_name);

        if (savedInstanceState != null) {
           //...
        }

        return view;
    }
}

If you want to read the complete source code, please go here on github

jww
  • 97,681
  • 90
  • 411
  • 885
Logan Guo
  • 865
  • 4
  • 17
  • 35
  • possible duplicate of [Two public classes in one file java](http://stackoverflow.com/questions/11177676/two-public-classes-in-one-file-java) You can do this and Java compiler will split into 2 .class files. It's messy though, and not a great idea. It's just a design choice. See also: http://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file – RossC Aug 21 '14 at 10:40
  • 2
    Only one of them is public. – Thorbjørn Ravn Andersen Aug 21 '14 at 10:47

2 Answers2

1

I was confused very much that why two isolated classes can exist in one java file.

The Java Language Specification allows you to declare multiple top-level types in a compilation unit (e.g. a file)

The Java Grammar (see JLS 7.3) says:

CompilationUnit:
    [PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

where [ ... ] means optional, and { ... } means zero or more.

Furthermore, a compiler is permitted to make the following restrictions:

  • A file contains at most one type that is public and/or that is referred to by a different compilation unit.

  • The name of said type matches the name of the source file.

As JLS 7.6 explains, these rules make it easy for the compiler to determine which source code files should be examined when looking for a class / interface referencedby some other compilation unit.


What is this used for?

Well, most Java programmers don't use this. Most Java programmers put each type in a separate source file anyway.

However, some people prefer to not to have their source code in "lots of" separate files.

The "multiple top-level types in one file" feature comes from Java 1.0, where there was no nesting of types. Nested types make this feature redundant.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

as you can see only one class is public and the other one is default which means there's no compilation error, and you can add as many classes in one java file the only restriction is that only one top-level class should be public so that the compiler can give the source file the name of that public class. I think the reason why the author of the source code did this is because he Chose to write his code like that and even though there's no difference in dependencies between the two classes either you write it this way or you separate them (separating them is recommended) I think maybe he considered them as one class.

Namine
  • 35
  • 2
  • 10