aspose file tools*
The moose likes Beginning Java and the fly likes Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton Pattern" Watch "Singleton Pattern" New topic
Author

Singleton Pattern

Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
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

Joined: Feb 21, 2002
Posts: 91
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

Joined: Jul 11, 2001
Posts: 14112
Actually, you need to *remove* the return type, as a constructor doesn't have one.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

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.


[How To Ask Questions On JavaRanch]
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91

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

Joined: Dec 10, 2001
Posts: 7023
Take a look at The Case of the Disappearing Constructors.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Pattern