Multiple classes in the same eclipse window - java

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?

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.

I would say the frustration are not genuine because:
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)
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.

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.

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.

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

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.

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 (:

Related

Matlab's Tab completion for classes

I have the following project on my hands, and I am banging my head to the wall for this "little" caveat.
In the project Matlab classes are used. Due to the structure of the project, I have the folders structured as follows:
+a/+b/+c/
Then, on c there are a bunch of other subfolders declared:
+a/+b/+c/+d
+a/+b/+c/+e
+a/+b/+c/+f
+a/+b/+c/+g
On one of those folders (let's sat +e) is where I am implementing my .m classes, which contain properties, as well as Static methods:
+a/+b/+c/+e/my_class_1.m
+a/+b/+c/+e/my_class_2.m
+a/+b/+c/+e/my_class_3.m
+a/+b/+c/+e/my_class_4.m
So let's take a look into my startup.m file:
% add the path to the class
addpath(genpath('<previous_path_to_a>'));
% import the module
import a.b.c.e.*
What I would like to do now is to be able to press my_class_1. + Tab on the Matlab prompt and be shown the properties and methods available for that given class.
I know I could just use Matlab's methods() function for this, or the properties() one, but it would be really nice to be able to just type:
help my_class_1. + Tab
to be able to select the given method and see it's documentation.
Otherwise I have, as I said, to call methods() first to see what the names of the class's own methods are for this particular class, to be able to access its documentation.
Edit:
Of course, what does work is typing the whole path, in my example:
help a.b.c.e.my_class_1. + Tab
The question is how to get rid off those previously annoying a.b.c.e.
Hmm, looks like you're right. Tab completion of methods and properties only seems to work with fully qualified class names, even if the class is on the path and imported.
I don't know of a workaround. If I were you, I'd enter an enhancement request with MathWorks for that. It would seem like an obvious and nice thing to have.

How do I import and use a class that is in a specified path?

I am working on a program, which is supposed to have "Modules" which people can make their own code from an API, and they can then add one of their Modules, let's say it's called ModuleNicerText, they add it to the MyProgram\Modules folder as a .java class (the source), and the class implements Module.
The Module class has one method (well, it has more but for the sake of this post I'll say one), called onUpdate(), and I want to be able to somehow import that class to my code, and then run the .onUpdate() whenever I need to, I hope I explained it well enough, if I haven't then here is another example:
I have an interface, named Module
this is the body of Module:
public interface Module {
void onUpdate();
}
Now, I have my main class named MyProgram, and in that class I have this method:
public void onUpdate(){
for (Module module : modules){
module.onUpdate();
}
}
I use this kind of system, so I can easily add or remove components without having to mess with a bunch of stuff, if it is all crowded in one class/method.
Now, there is a directory that gets created when the program is run which is MyProgram\Modules and anyone who wants can add a .java file which extends the Module interface there.
I want to be able to look through the .java files in that directory and be able to call the .onUpdate() method for them, so that my users can have as much customization as they possibly can!
Any help at all is very much appreciated, and I thank you in advance for taking your time to read this enormous wall of text :)
There is a good article (a little old, but great explanation).
It provides detailed information about how to use the Proxy Pattern through UpToDate file check.
By the time you keep the same methods inside your interface, and change only implementation of it, you can make use of this technique only in classes which implements the interface.
Check it out: Dynamic Java code to your Application
You can use Java Simple Plugin Framework. It does something similiar to what you want and is ready to use.

How to change references of a class to another class in eclipse

I have created a new class out of an old class with different method implementations. Now, I want to update all the references of that old class to this new class. Is there anyway to do this automatically in eclipse?
I can actually do a rename(with update references) to this old class and copy the new code into this but the entire code being in source control makes my life a tougher with this process.
I am just wondering if there is an easy way to do this and have both classes at the end and have the references updated to the new class.
Using the refactoring functionality should do the trick. Refactor the old class into the new one:
Right click on the class name, Refactor > Rename > Put the new name of the class > Press Enter.
This should do it.
I don't know of any existing refactoring that does exactly that, but it's simple enough to:
Temporarily rename your new class to something else.
Do a refactor/rename of the old class to the new class name.
Manually (non-refactor) rename the old class back to its original name.
Manually (non-refactor) rename the new class back to its original name.
That way you get the power of the rename refactoring to help you update the references, but in the end, the classes are still the same as they were.
right click your class file select refactor then rename it what ever you want ..
My guess is that person asking the question is using something like Perforce or ClearCase where temporarily renaming files is a pain.
The best bet is to do a simple Eclipse global file search and replace.
Click on Search -> File
and click on the Replace button on the bottom.

How to parse a Java file and change the name of a variable/method name/class name automatically?

I would like to know which is in your opinion the best way to parse a Java file and automatically change either a variable name, a method name or the class name. I would like to do so because I want to offuscate some code by just changing one small part of it (one of the three cited above).
For example I could have a class that has a global variable public static final int index = 0 and I would like to change it to public static final int xxx = 0. Of course i should be replaced to xxx in each occurrence in the file. Same goes for class name or method name.
I've been told ANTLR may work for me, but I've never used it and I don't want to waste time learning it if then I discover it's not suited for my purpose.
Thanks
EDIT:
I do not need to obfuscate the code, I need to change a small part of it. Either one of those mentioned above.
If you only need to change a few such identifiers, then refactoring (supported by most IDEs, including Eclipse) is simple, quick, and reliable.
If you need to change a lot (for example, if you're trying to replace uses of english with roughly equivalent identifiers in a foreign language - e.g., counter => compteur), then I'd personally find myself using something scriptable, like sed or Perl. And I'd be very careful to make sure I was hitting exact matches (e.g., when changing lie to truth, that I don't also change belief' to 'betruthf)
One caution if you go with an automated, do-a-lot-at-a-time solution: be very sure you can test functionality before and after to assure that you haven't broken anything.
Maybe the easiest is to use an existing code obfuscator, like the free Proguard :
http://proguard.sourceforge.net/
Use a code obfuscator to do the work for you.
Or use an IDE like Eclipse, which has this kind of thing built in using the Refactor menu.

Can eclipse extract a second class in class file to its own file

I often refactor code first by creating an inner class inside the class I'm working on--When I'm done, I move the entire thing into a new class file. This makes refactoring code into the new class extremely easy because A) I'm only dealing with a single file, and B) I don't create new files until I have a pretty good idea of the name/names (Sometimes it ends up as more than one class).
Is there any way Eclipse can help me with the final move? I should just be able to tell it what package I want the class in, it can figure out the filename from the class name and the directory from the package.
This seems like a trivial refactor and really obvious, but I can't figure out the keystrokes/gestures/whatever to make it happen. I've tried dragging, menus, context menus, and browsing through the keyboard shortcuts.
Anyone know this one?
[edit] These are already "Top Level" classes in this file, not inner classes, and "Move" doesn't seem to want to create a new class for me. This is the hard way that I usually do it--involves going out, creating an empty class, coming back and moving. I would like to do the whole thing in a single step.
I'm sorry I gave the wrong answer before. I rechecked, and it didn't do quite want you want. I did find a solution for you though, again, in 3.4.
Highlight the class, do a copy CTRL-C or cut CTRL-X, click on the package you want the class do go into, and do a paste, CTRL-V. Eclipse will auto generate the class for you.
Convert Member Type to Top Level doesn't quite work. Doing that will create a field of the outer class and generate a constructor that takes the outer class as a parameter.
In Eclipse 3.6, you can do: Refactor -> Move type to new file
Right-click the class name (in the source code) and choose Refactor -> Convert Member Type to Top Level. It doesn't let you choose the package, though.
For IntelliJ IDEA / Android Studio:
Refactor -> Move -> Move inner class MyInnerClass to upper level
Can be done in 2 refactorings :
Convert Member type to top level
Move

Categories

Resources