Yes, this is not a Servlets question. Just because you are building a Web Application that could mean anything. Meaning not just Servlets. There is JSP, HTML, XML, DHTML, JSP, PHP, CGI. But none of those are Servlets. So none of those would be in the Servlets forum.
OK, so why does the class being in a Jar mean anything different than it not being in a jar? Except only that you might not be able to change the code. But then you could use a decompiler and get that .class file out of the jar, decompile back to Java, change the code, re compile, and put that new .class into the jar file.
The only thing I might point out in your code, is that you might want to use a Factory to either get an LDAPManager or an instance of SearchUser. But either way the code looks fine as is. I would prefer to always stick with an instance variable more than static variables. Meaning only in certain situations will you use static variables in application. OK, that explaination is vague, but basically what I wanted to say.
Moving this to the Java In General (Beginner) forum. Might be moved again to intermediate, but I'll leave that to the bartenders there.
Since you are creating objetcs inside the methods, there won't be any big difference. Once these methods are executed ldapManager again holds a null reference. Suppose if you have your code like this, then a little bit more memory would be used by the JVM.
Is it ok if I declare a class level "LDAPManager" variable and reuse the same in both methods ?
or would it be better to define local variables in each of the two mwthods ?
Could someone shed some light on the advantages and disadvantages ?
An object of the class LDAPManager can be created one for the application, one for the user session or one for each request. Below I will explain each.
If once for the application you can use a singleton pattern. Considerations should be given to thread synchronization. Because multiple threads will be using the same object. The Object LDAPManager will be created only once in the application.
If once for the session, you will be creating an LDAPManager per user.
If once for the request, every similar request coming to the server will create a new LDAPManager. Memmory is concern here. GarbageCollection is another.
[bold] So if you reuse the object, synchronization is issue. If you create separate instances, memmory management is the issue. [/bold]
In your code one LDAPManager will be created for every SearchUser object, if getuserbyid or searchusersByEmail is called. How often you create SearchUser is not obvious from above code. So an answer cannot be given with the information provided.
If the methods ldapManager.findUserByEmployeeId or ldapManager.findUserByEmail are thread safe, having LDAPManager once per application will be enough. If it is not, consider session or request scope.
In servlets there are specific objects which represents session and request. (This discussion thread started from servlets). You may use these objects to maintain the scope of the LDAPManager.
Few suggestions for improvement..
Since you are creating objects inside the methods, there won't be any big difference. Once these methods are executed ldapManager again holds a null reference.
This is not correct. Since LdapManager is a class variable of SearchUser, the reference to LdapManager will not be lost unless you specifically code
before the end of each method where ldapManager is initialized.
You may create a method called fetchLdapManager to get an instance of LdapManager. The code may look as follows. By this you will not have to repeat the null check code.
By making the ldapManager a method variable, you will be creating and removing the LdapManager each time the method executes. This will give way to allocate and deallocate lot of memmory. But is extremely thread safe. In java garbage collection is an issue. It stops the entire system (not in 1.5 if you configure in certain way) to run garbage collection. Unnecessary creation of objects need to be avoided to reduce garbage collection.
If the methods in LdapManager are thread safe don't use ldapManager as a method variable.