RcK
RcK

Reputation: 97

Java Object Initialization many times in a Class or just once?

I want to know whats the best practice when it comes to Class Initialization,

I mean should I initialize a Class once Customer c = new Customer(); in the top level, and use it everywhere in the class:

Tools tools = new Tools();
    public boolean doCIdCheck(int cId) {
        final Iterator<Customer> cursor = tools.Customers.iterator();
        while (cursor.hasNext()) {
            if (cursor.next().getCIdCheck(cId)) {
                return true;
            }
        }
        return false;
    }

or should I just use new Customer().checkCId(); where ever I need it:

    public boolean doCIdCheck(int cId) {
        final Iterator<Customer> cursor = new Tools().Customers.iterator();
        while (cursor.hasNext()) {
            if (cursor.next().getCIdCheck(cId)) {
                return true;
            }
        }
        return false;
    }

Or best to have each function/method have its own instance of a class:

    public boolean doCIdCheck(int cId) {
        Tools tools = new Tools();
        final Iterator<Customer> cursor = tools.Customers.iterator();
        while (cursor.hasNext()) {
            if (cursor.next().getCIdCheck(cId)) {
                return true;
            }
        }
        return false;
    }

Upvotes: 2

Views: 1091

Answers (4)

MaVRoSCy
MaVRoSCy

Reputation: 17839

Taking into consideration that you will need that Object in every method of the class and taking into account only memory management and processor consumption then:

1.Option 1 (Keep a global variable accessible everywhere ): Memory usage will increase if you have significant amount of these objects, as you will store an extra Customer Object for each instance of Tools. This will gain you processor speed because you don't have to create and destroy Customer Objects for every method.

2.Option 2: (Keep a Customer object accessible only within method ): Memory usage will decrease and your application will become more processor intensive as you will create and destroy objects each time a method is accessed.

IMO if you instantiate a LOT of Tools objects then go for option 2 else if only a small amount if Tools Objects exist go for option 1

Upvotes: 0

Amit Deshpande
Amit Deshpande

Reputation: 19185

You can override equals() method to check equality on cid in Customer. Example

And then instead of those loops you can use contains() of Collection method

So your doCIdCheck method will look like

public boolean doCIdCheck(Customer cId) {
    Tools tools = new Tools();// I don't know if you are initializing list
                                // in Tools constructor or not but it should
                                // be initialized in constructor or in declaration
    return tools.customers.contains(cId);
}

Upvotes: 0

Keppil
Keppil

Reputation: 46229

From your example, it looks like the best approach would be to declare the shared methods and members of Tools as static instead of creating an instance, and just call them as

final Iterator<Customer> cursor = Tools.Customers.iterator();

This of course assumes that you don't store data in Tools that could cause conflicts, in that case creating multiple instances is preferred.

Upvotes: 1

developerXXX
developerXXX

Reputation: 689

Its depends on your requirement. For example if you don't want the class Tools outside of the method doCIdCheck, then 3rd options is preferred.

Upvotes: 0

Related Questions