Beginner Question: Is it possible to put each class into it's own java file in Eclispe?

by Dianne Hackborn » Wed, 21 Apr 2010 05:54:02 GMT

Sponsored Links
 The Java compiler requires that each top-level class be in its own source


Beginner Question: Is it possible to put each class into it's own java file in Eclispe?

by ~ TreKing » Wed, 21 Apr 2010 05:54:23 GMT


Yes. Which you could have found out on your own in about 5 minutes.

TreKing - Chicago transit tracking app for Android-powered devices 


Sponsored Links

Beginner Question: Is it possible to put each class into it's own java file in Eclispe?

by patbenatar » Wed, 21 Apr 2010 16:49:36 GMT

 "but in all of the examples I've seen for android all of the methods
and classes are all in one big java file"

Uhhmmmm........ On what sites did you find these examples?


Beginner Question: Is it possible to put each class into it's own java file in Eclispe?

by Bob Kerns » Thu, 22 Apr 2010 04:27:09 GMT

 This may or may not be more information than the OP needed. But there
may be confusion there worth clarifying).

Actually, Dianne, that's only true of top-level classes with the
'public' modifier. You can have top-level classes that are package-
private in the same file -- but no more than one 'public'', and the
file must be named with the same name as the class, plus the ".java"

However -- that is ALMOST never done!

Binxalot -- I'm afraid you're mistaken -- I'm reasonably certain that
you have NOT seen all the methods and classes in one big java file.

Either you've misunderstood what you were seeing, because it was all
on one web page, or something else. Let's not worry about any
confusion from web page formatting...

If you were actually looking at .java files, then you were probably
thrown off by one of these points:

*) A java class definition cannot be split between files. All methods
and fields will appear within the same file, which will contain the
*complete* definition of that class.

*) A java class definition can contain INNER class definitions. These
are not independent definitions. If they do not include the 'static'
keyword before the 'class' keyword, they can only be instantiated in
the context of an instance of the outer class. If they DO include the
'static' keyword before the 'class' keyword, then you can view them as
being somewhat akin to namespaces in intent. That is, the class name
is available within the scope of that class; to be used elsewhere it
needs to either be explicitly qualified with outer class name, e.g.
(Outer.Inner, or, or imported with the
'import static' statement.

If you don't wish to do this, then you can instead use packages and
make these inner classes top-level. Eclipse provides convenient
commands to do this.

Note that inner classes that have the 'public' modifier can be
referenced outside the package, and those that have the 'protected'
modifier can be referenced by subclasses of the outer class. Those
with 'private' can only be accessed within the class, and those with
no modifier can only be accessed by other classes within the same
package. This is a bit more flexible than top-level classes, which can
only be 'public' or default (i.e. private to the package).

I hope this helps you read the code a bit easier. It's best to think
of one .java file as being one complete unit; there may be inner and
private classes, but it provides only one top-level class to other
packages, and it provides the complete definition of that class.

The compiler will output each class to its own .class file, including
inner classes. They'll be in the same directory as other classes in
the same package, but with names constructed to avoid conflicts.
Anonymous (nameless) inner classes will get generated names. So all of
these .class files have to be packaged up in an Java application.

On Apr 20, 2:53pm, Dianne Hackborn <> wrote:

> >

Beginner Question: Is it possible to put each class into it's own java file in Eclispe?

by Dianne Hackborn » Thu, 22 Apr 2010 04:31:27 GMT

 h good point.  In fact I have had non-public classes combined in a public
class's file. And usually quickly regretted it. :)

On Wed, Apr 21, 2010 at 1:26 PM, Bob Kerns <> wrote:

> > >

Other Threads

1. Operator internet unlimited

Dear RR,

Hi, Salam kenal. Saya pengguna baru android. Mau tanya2.. RR pakai operator
apa ya?

Sedang compare2 yang paling menguntungkan.

Thanks all

"Indonesian Android Community [id-android]"

PING'S mobile
Email:  Ph. (021) 96087100
Yopie Ratjoen
Email:  Ph. 08977726681
Gila original
Email:  Ph. (031) 91555898

Aturan Jual/Kloteran ID-Android

2. From emulator to real device

HI all,

I can run the app on the emulator without issues - when I run it on a
real device
I get the exception that certain classes are not found (see below for
I am sure this is a loaded question and I know an emulator is not a
real device
and many things can alter their behavior during this transition but
loading classes
seems a bit fundamental - any ideas? Perms and everything else in the
directories are identical. I am just wondering if anyone else has
seen  similar
issue before and if they have any hints as to what to look for -
thanks again

I/System.out(  297): P2PA - ########## - fatal error : group creation
W/System.err(  297): exception.PeerGroupException: Could not locate
World PeerGroup Module Implementation.
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at android.os.Looper.loop(
W/System.err(  297):    at
W/System.err(  297):    at
java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(  297):    at java.lang.reflect.Method.invoke(
W/System.err(  297):    at
W/System.err(  297):    at
W/System.err(  297):    at dalvik.system.NativeStart.main(Native


3. Library SharedPreferences

4. Is there any way to hide a dialog yet have it remain active?

5. Creating a trial version

6. Equivalent to dialog display iniitiated from a service

7. Ask: testimoni Barry Abdul?