Viktor Apoyan
Viktor Apoyan

Reputation: 10755

Use two classes in the same java file

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).

Upvotes: 0

Views: 2956

Answers (7)

Sandeep Pathak
Sandeep Pathak

Reputation: 10767

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503489

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?

Upvotes: 1

gprathour
gprathour

Reputation: 15333

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.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258648

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.

Upvotes: 2

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

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.

Upvotes: 3

Jean Logeart
Jean Logeart

Reputation: 53859

You cannot.

What you can do is to have inner classes.

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60711

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

Upvotes: 1

Related Questions