What do you think?

Hi forums, you may have seen me around here, as you can see, I know almost 0 coding. So I decided to go into java, here is the repo with my first java thingy. https://github.com/TheDoctorsLife/HelloWorld
I plan on adding a GUI at some point and generally make it better. any help towards increasing my java knowledge is appreciated.

+1 for good conventions

Edit: Stupid auto correct :confused:

1 Like

Your package should be com.thedoctorslife not thedoctorslife.com

The inconsistent formatting, specifically indentation, makes it harder to follow.

Do not mix naming conventions (e.g. method Hello starts with capital but end is lowercase). In java, method names should look like someMethodName

4 Likes

two src folders? it looks like you’re trying to emulate a maven directory structure. research what maven is and try with that instead of manually making your own folders.

also, package naming. package names usually start with a reverse domain name, followes by whatever. thedoctorslife.com should be com.thedoctorslife. usually you have the project name in there too, to differentiate between different projects. (e.g. com.thedoctorslife.hello) its usually expected that the package (the website part of it, anyway) actually points to an existing website/domain, but thats not always the case (like my me.scarlet packages).

and yeah 10/10 for nice conventions~ ;ww;

EDIT: dare you ninja me when im typing on my phone, @simon816

3 Likes

Thanks for that, I have fixed the above issues and have pushed to github.

I was looking in Stuff.java when I noticed:
public static void Hello() {
Your methods should be camelCase, like so:
public static void hello() {
And
public static void methodHere() {

This was me a year ago, +1 for courage. I’ll help you in any way possible :slight_smile:

2 Likes

My recommendation is to find some well known open source Java programs. Make sure the ones you pick are also known for having good coding standards and conventions. Download the sources and copy them line by line by hand until you get used to typing in that convention. After it becomes a habit, look up standard conventions for Java and why they are the way they are. You can avoid convention entirely and still have something syntactically correct. It just won’t be readable or user friendly.

You’ve got both spaces and tabs for indentation. Pick one, not both :wink:.

I meet halfway and convert tabs to 4 spaces. Works basically everywhere and still allows me to use the tab key for indentation.

Glad to see people getting into dev world :smile: especially if you plan on becoming a Java developer, not a Sponge/Bukkit/any-Java-API-“specialized” developer. This will help you making things clean, a lot.

May I suggest:

  1. Be consistent in your indentation: add a single indent into each block, add an empty line after each block, put the closing brace at the same indentation as the line opening the block
  2. Still for code formatting: use a code formatter (in Eclipse: Ctrl+Shift+F) this should make your life easier :wink:
  3. Like said @ButterDev, method names should always be camelCased with lowercased first character
  4. Your Stuff class is conventionnally called an “Util”, as it is only made of static methods. You should call it StuffUtils and make the class final because it would have no sense to make it having children (public final class)

I’m not a Sponge developer and I don’t know Sponge’s team best practices. I just use Java at work as a specialist and these are just good practices in any situation.

Btw on point 4.: I don’t know in Sponge but in Bukkit I saw a LOT the usage of static methods both in Bukkit and in plugins. I think that’s a bad practice as it avoids the benefits of object oriented programming.

4 Likes

Thanks, I wish I knew about the code formatter earlier, anyways I updated github with those changes.

@DarkArcana Do you have any recommendations?

adding on pt.4, should probably add a private constructor so it couldnt be initialized either

/*
 * No instance 4 chuu.
 *
 */
private Stuff() {}
1 Like

Both files are in the same package, so an import in HelloWorld.java is not needed. I created a pull request.

No need, I have already fixed that.

how would I do that?

I think that instead of asking you should look more in depth at example code like I said above. You’re off to a good start, but you can’t get any further without some more practice. Make something fun. If you want to do webdev, try Spring. If you want to do gamedev, try libgdx. There are a lot of different frameworks and things to try and play around with to get a feel for how best to use Java.

1 Like

The purpose of a class is usually to be instanciated: to make objects from it (see a class as a definition of something)

StuffUtils is an utilitary class, that bundles methods only. This can’t represent an object, that’s why it should not be instantiated, But the default constructor of each class is public: so the trick is to define a private constructor with nothing inside (or just a comment like “private constructor”)

I agree with DarkArcana on this: do learn and be curious. However I’d suggest you to start from docs. I’ve not begun with official docs, but they don’t look that bad. I’d personnally start from here.

We’re still here to answer your doubts though :wink:

-points to my code example-
private scope on the constructor will prevent anything outside the class from instantiating it as an object. note “outside the class;” nested subclasses may still instantiate it.

look up official jdocs on scope/visibility modifiers .w.