stensootla
stensootla

Reputation: 14845

Multiple classes in the same eclipse window

I'm reading Thinking in Java and it's frustrating to declare each class in a separate window in Eclipse, as the examples often contain 6-7 very simple classes.

I can just make a new class file, make one class public in this class file and the others with default access, but I don't know what should be the class' name I created. For example, I do the following:

New -> Class -> and then I must choose a class name, let's say it's Dog.

Now, in this file, I have this:

public class Dog {
}
class Cat {
}

But since I have two classes, it's a little weird to have this class file (I don't know if it's the right word here?) to be named Dog in Eclipse (The name in the src folder).

Is there a better way to declare multiple classes in the same window(?) in Eclipse?

Upvotes: 0

Views: 3928

Answers (7)

Kyle Breton
Kyle Breton

Reputation: 361

Unfortunately you do have to do this the long way, as everyone else has suggested / insisted. If the problem is a matter of clicking around through tabs, though, eclipse does allow you to drag tabs into new windows on the screen, which lets you view potentially all of them at once.

You also end up with an "overview" of the classes in the file explorer on the left of the screen, if that's more along the lines of what you're looking for.

Good luck (:

Upvotes: 0

Guillermo Merino
Guillermo Merino

Reputation: 3257

Don't mix Eclipse window with files, you can understand a .java file as a container for a java class. It's the standard way and it would help you to have a more clear project when it becomes bigger.

You can have more information about this here

If you want 2 classes in the screen you can split the eclipse editor window by dragging the opened tab file and drop it on the tabs zone.

Upvotes: 0

Sandeep Jindal
Sandeep Jindal

Reputation: 15338

I would say the frustration are not genuine because:

  1. This is the how Java is designed and makes all sense to define each class in a separate file. (Unless you want to write your own compiler)

  2. You may want to use some shortcuts e.g.

    • Cntrl + Shift + R` to search a class
    • Alt + Shift + R to rename
    • You can update Eclipse to use shortcut for switching within classes.

Upvotes: 1

Russell Uhl
Russell Uhl

Reputation: 4531

To actually answer your question, rather than leave a bunch of comments stating why you shouldn't (which you seem to understand already), no. There isn't really a better way to do what you want. I don't know if it will compile or not (I seem to recall seeing that in the past in Java 5), but KyleM seems to think not so we'll go with that.

Short answer: no, there is not a better way to declare multiple classes in the same file.

(I don't want to suggest inner classes because that is kind of complicated for someone just starting java, as your post suggests).

Upvotes: 0

Kyle
Kyle

Reputation: 4288

What you're doing isn't going to compile. Each top level java class must be declared in a file with the same name. It will give you an error "Cat must be declared in its own file" or something like that. If you really want to, you can put the Cat class inside of the Dog class, which is called an inner class. However since they aren't related classes you shouldn't do that. Just declare each one in its own file.

Upvotes: 1

xeranic
xeranic

Reputation: 1411

Keep each class in it's own position. If your class is small and data can be exposed you can consider using nested (inner) class.

By the way, in Eclipse you can show multiple class at same time. Just drag you file title to some place.

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30809

A java file can have at most only one public class into it. And the name of that file should be same as of that public class.

Upvotes: 2

Related Questions