0

I have file TestClass.java

package com.fido.android.sample.dsm.SoftPin.Core;

public class TestClass
{
   public int mValue1;
   public String mValue2;
}

Now in this file (TestClass.java) I want to declare one more class, but when I write for example:

public class SecondClass
{
    // Class members goes here.
}

Compiler do not allow me to do that, if I remove public everything is Okay, but I can use SecondClass only in the TestClass.java, I can't write

SecondClass sc = new SecondClass();

out of TestClass.java class. Now I want to know if there is a way to do such thing, to have two classes in the same file and to use them from everywhere (not inner classes).

2
  • 1
    One class per file is a design feature, not a bug. Part of the design is making it easy to tell what class is in what file (ever try making a class named differently from its filename?) Commented Nov 28, 2011 at 9:52
  • possible duplicate of Java: Multiple class declarations in one file Commented Nov 28, 2011 at 10:03

7 Answers 7

3

Question is: Why would you want to declare a second public class within the same Java class file? It is a rule in Java that each public class must be declared in a single class file - except for nested classes like Graham Borland pointed out.

Sign up to request clarification or add additional context in comments.

5 Comments

+1, but if second is not public, it mean I can't use it from other places ?
Yes, the public class may require additional classes internally (for example to build internal lists, internal event handling etc.). You may declare such classes within the same class file. As they are only required by the public class, there's no problem and also no need for them to be available publicly. As soon as you want classes to be public to all other classes, they must be declared within their own file - this is by convention is Java.
It's not a required rule - it's only a rule which a compiler may enforce. You could write a compiler which allowed this and it would still comply with the JLS.
@Jon: Thanks for pointing this out. I was, however, referring to the usual JDK, which in my experience has always included a compiler which enforced this rule.
@ThorstenDittmar: Well, you referred to "Java", rather than a specific implementation ;)
2

Short answer: You can't.

That's how Java works.

You can only declare a single public class per file, with the class name the same as the file name.

You can use inner-classing as Graham suggested, or better yet, move the second class in a new file.

Comments

1

If you have SecondClass inside TestClass (i.e. nested inside the class definition), with public visibility, then you can refer to TestClass.SecondClass everywhere.

5 Comments

I know that, but it is not a solution in my why.
I want to declare two classes in the same file and use them. Like in C or C++.
It answers your question "I want to know if there is a way to do such thing, to have two classes in the same file and to use them from everywhere.". If that is not actually your question, then please fix your question.
You can't. As I said, why would you want to do so? What's the problem having two files?
As I am not JAVA programmer and in C C++ I can do such things (to have more then 2 public calsses in the same file) I want to do same thing in Java !
1

No, you can't, if the compiler chooses to enforce this rule from the Java Language Specification, section 7.6:

When packages are stored in a file system (§7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

  • The type is referred to by code in other compilation units of the package in which the type is declared.
  • The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit.

So this is optional in that it's still "legal Java" to include more than one public top-level class in a single source file - but it's also valid for the compiler to reject it. In practice, I think every file-based Java compiler I've ever used enforces this rule.

Now you could try to find a different compiler if you really wanted, but there's a reason for this: Java programmers are used to finding the source code for a public top-level type (and usually any top-level type) in a source file with the same name.

To ask a return question provocatively: why do you want to make your source code hard to navigate?

Comments

0

You cannot.

What you can do is to have inner classes.

Comments

0

According to java conventions, A public class should be created in a separate file having same name as of class name.

So you cannot make two public classes in same file.

you can try either removing public from one class or making inner class.

Comments

0

Since public classes must have the same name as the source file , there can only one pulbic class inside a java file.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.