Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class Misuse - Best Practices [closed]

I'm fairly certain that I am not doing something class related correctly.

I am using a class to create a set of variables (like a javascript object sort of maybe but not really).

I am using it as shown bellow (a basic example)

public class myScript {

    public static void main(String[] args) {

        Client   warehouse          = new Client();
        Contacts warehouseContactsA = new Contacts();
        Contacts warehouseContactsB = new Contacts();

        warehouse.idno = 1;
        warehouse.name = "warehouse";
        warehouse.desc = "I don't exist";

        warehouseContactsA.client_idno = 1;
        warehouseContactsA.email       = "[email protected]"

        warehouseContactsB.client_idno = 1;
        warehouseContactsB.email       = "[email protected]"            

        insertIntoDB(warehouse,
                     warehouseContactsA,
                     warehouseContactsB);

    }

    public static void insertIntoDB(Client   warehouse,
                                    Contacts warehouseContactsA,
                                    Contacts warehouseContactsB) {

        // code to insert into database here

    }

    private class Client {
        int      idno;
        String   name;
        String   desc;
    }

    private class Contacts {
        int      client_idno;
        String   email;
    }

}

Is there any reason to not use classes this way and if so is there a simpler way to store/manage data that doesn't require a class?

like image 888
TheLovelySausage Avatar asked Jan 28 '26 10:01

TheLovelySausage


1 Answers

Creating inner classes is probably going to create pitfalls for you. When you don't define them as static then they require an implicit reference back to the outer class, which your code doesn't need, it will only get in the way and cause obscure errors. Maybe you're doing this so you can compile only one class and avoid having a build script? Simple build scripts that use gradle are trivial (not like the bad old days when we used ant), so that shouldn't be an issue. It would be better to move your persistent entities out into separate files.

What you're doing with database connections and transactions is not clear. Generally it's bad to try to do each insert in its own transaction, if only because each transaction has overhead and it increases the time the inserts need to run. Typically you'd want to process the inserts in batches.

Mainly, though, if you're writing a script, use a scripting language. Groovy could be a good choice here:

  • you wouldn't need an outer class for the procedural script part
  • you can define multiple public classes in one file
  • Groovy includes a groovy.sql api for simplifying JDBC code.
like image 148
Nathan Hughes Avatar answered Jan 31 '26 00:01

Nathan Hughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!