This is a question from newbie in both Java and Netbeans. I have searched a lot in google before posting it here.
I am using netbeans to create a gui application. In the standard books, the structure for java coding is suggested as,
class className{
field names
constructor(){
}
method1(){
}
method2(){
}
main method(){
}
}
In Netbeans,
class ClassName{
constructor(){
initComponents();
}
initComponents(){
}
//autogenerated code for methods related to swing actions
action1(){
}
action2(){
}
main method(){
}
field Names;
}
My question is, where do i write method1() and method2()? Should i have to put the fieldnames at the end or on top? - the autogenerated fieldnames cannot be edited. So, should i have to write the field names which i declare on top or at the bottom. I know that anywhere will work. But i want to make sure i am coding them at the right place. Thanks
Some if this is personal preference. Some people like the fields at the end of the class, I personal like them at the start.
I tend to put the constructor first, followed by the methods and allow the auto generated code to sit towards the bottom and I put inner classes at the end.
But that's just me.
I'm not sure how Netbeans works, but if it generates code in your file, perhaps you should store your own functions somewhere else, this way you can freely choose your structure without having Netbeans taking up a section of your file.
In fact, you can write your methods wherever you think the best place is.
For Swing app, Netbeans will use this rather awkward class structure you've just showed - with the instances variables, constants and the like at the bottom of the class.
Personally I prefer to put the constants at the top, followed by variables, constructors, public, protected, default and private methods (in this order) - then any inner class (if there is any).
There must be a way to change Netbeans class template - but I've never digged into the templates setup of Netbeans for the classes structure - only for the header's comment.
Related
I am using the StdDraw.java library and I can't edit the file. I want to add various items to the JFrame such as JMenu, buttons and others without compromising the canvas and the Jframe.
I have tried something like
StdDraw.class.getMethods"
Yet I can't seem to get it to work. It seems I can only use the methods inside the class and don't add some of my own or edit the ones already in.
The file is available online. How would I be able to achieve the above?
StdDraw.java is a final class, if I am not mistaken. A final class cannot be extended. So you have two options:
You can directly use source then add your own attributes and compile it yourself, if the license permits this kind of usage.
You can encapsulate StdDraw.java class, with your own wrapper class and direct method calls using Java Reflection.
Use inheritance.
class MyClass extends JFrame {
String myField = ""; //--- This is my own field!
}
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 (:
This may seems a silly question for Java developers, however, I'm new to Java, and my background is from low level c.
I used to include an header file with all the constants that were relevant for my projects. (usually #define's).
I'm working on a big Java project now, and there a few constants I need to make global (they fit in more than one class, and used in various parts of the project )
It makes it hard for me to decide where to put it, should I declare the same constant few times, one in each class ?
A lot of framework, uses XML files to declare constants & definitions for the framework (Hibernate, Log4J, etc.) Is it wise to use this kind of technique in my project ? if so, how can it be done easily ?
As with many things, there are many ways to do it. One thing you should not do is declare them multiple times - that's just plain silly. :P
Everything has to be in a class in Java, so either:
Pick a "main" class (say I have a project called "FTPServerApp" - I could put them there)
Create a "Util" class that contains all of them
When you figure out where to put them, declare them all this way:
public static final [type] [NAME_IN_ALL_CAPS] = [value];
This will
make them available to all your project code, anywhere (public)
only one copy of the value exists across all instances of the class (static)
they cannot be changed (final).
The ALL_CAPS_FOR_CONSTANT_NAMES, separated by underscores, is the convention in Java.
So, if this was declared in a class called FTPServerAPP, and you had a constant called SERVICE_PORT it might be:
public class FTPServerApp {
public static final int SERVICE_PORT = 21;
...
}
...and you would access it, from any class, like this...
FTPServerApp.SERVICE_PORT
Take a look at enumeration types (http://download.oracle.com/javase/tutorial/java/javaOO/enum.html) They are supposed to provide a mechanism to supply constants without defining a concrete class (or an Interface with the desired constants, as is another option that people use).
One other technique I find helpful (similar to the FTPServerApp example given above) is to define a Context for whatever subsystem/component/etc... that holds not only the constants needed by components in that system, but can hold any state that you want to make more visible or don't want individual components to hold. I believe this is along the lines of one of the GoF patterns, but it has been so long since I have looked at that book that I can't be certain (and I am too lazy to look it up right now!)
I am having trouble finding the correct refactor option for the following scenario:
I have code like this (in Outer.java):
public class Outer {
// ... class stuff
public static class Inner {
// ... inner class stuff
}
}
I am looking for a way to select Inner, and have it converted to a top level class, in it's own .java source file. I know this is pretty easy to do manually with copy/paste etc., but the inner class is referenced in a lot of places, and I would like the refactor option to handle the change everywhere it is referenced.
I have tried the option Refactor -> Extract Class... but that does something weird with a field called data that I don't quite understand.
Eclipse version is 3.5.1 Galileo.
How do I refactor a static inner class to be a top level class?
Edit: Can't believe I overlooked the option to do this. Thanks for all your correct answers, +1 to you all. I'll still need to accept an answer, so if there is any more useful info, e.g. gotchas with the script, there is still a purpose to leaving an answer.
This is so easy I can't believe I missed it:
With the cursor anywhere within the inner type, right click and select:
Refactor -> Convert Member Type to Top Level...
(There is no shortcut in the default settings)
This automatically extracts the inner type, and places it in it's own file, in the same package and directory as the outer type.
Update
In later versions of Eclipse this refactoring has been renamed "Move Type to New File"
For completeness, version 4.x of Eclipse has changed terminology and now they call that operation as Move Type to New File...
Extract Class is not what you want.
I just did this using Eclipse 3.4.2
Select Inner
Right click 'Refactor->Convert Member Type To Top Level...'
I have 3.4.1, and if I right-click on Inner, >refactor, I have the option "Covert member Type to top level..", which does exactly what you want.
Use Refactor -> Convert Member to Top Level Type
For Intellj2018.1.
right click in the inner class
Refactor
Move
Move to upper level
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