Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern

 
Kamal Patel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am creating a Singleton class, so that only one instance of a object can ever be created. I am however getting an error with my code where I call the below command which is in my code below:
'instance = new ldapConnection(userID, pwd);'
I am not sure why it is complaining about the code - I am using an IDE and am getting a red line under these two lines of code meaning there is something wrong with it! Is it because I have not initialised my variables or something???

I have given my code below.
public class ldapConnection
{
/** Creates a new instance of ldapConnection */
private static DirContext instance = null;
private static DirContext ctx;
public static DirContext getInstance(String userID, String pwd)
{
if (instance == null){
instance = new ldapConnection(userID, pwd);}
return instance;
}
protected DirContext ldapConnection(String ID, String password){
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "url to resource");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid"+ID+", ou=Directory Administrators, dc=db, dc=com");
env.put(Context.SECURITY_CREDENTIALS , password);
try {
DirContext ctx = new InitialDirContext(env);}
catch(NamingException e)
{
//System.out.println("Search failed. <br>");
e.printStackTrace();
}
return ctx;
} // end of method

} //end of class
Thanks for your help.
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect it's because you are not calling a constructor.

returns a DirContext, not an ldapConnection which makes it a method rather than a constructor.
Try changing the return type and see what happens.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you need to *remove* the return type, as a constructor doesn't have one.
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kamal Patel:

I am not sure why it is complaining about the code - I am using an IDE and am getting a red line under these two lines of code meaning there is something wrong with it!

This is why using an IDE to learn Java is a Bad Idea. The IDE does a lot of work for you and you are at a loss when something goes wrong. Next time try compiling the class so you get an error message you can work with.
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Actually, you need to *remove* the return type, as a constructor doesn't have one.

Trying to walk and chew gum at the same time again
 
Dirk Schreckmann
Sheriff
Posts: 7023
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic