File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes singleton class methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "singleton class methods" Watch "singleton class methods" New topic
Author

singleton class methods

prasad krishnan
Greenhorn

Joined: Dec 03, 2005
Posts: 5
I have a singleton class say "A" inside A i have two methods methodB() and methodC(). Inside each methods i am creating a new object say objectD by using D's constructor by using "new" as

Singleton Class A {

methodB(){
D ObjectD = new D();
}

methodC() {
D objectD = new D();
}
}
This D is a normal java class. My issue is an user used the instance of A and made one objectD through methodB(). and if before the further processing of objectD if another user access methodC() and creates ObjectD would that result in overwriting of the already made objectD,

Any help will greately appreciated???
Thanks
Prasad
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

As you've shown the code, the answer is "no":, since objectD is a local variable in each method; local variables are confined to the method that declares them, and they go away as soon as the method returns.

If instead objectD was a member variable in A, then yes, this could be a problem. In that case, you may need to use thread synchronization to make sure only one copy is created (if you can't simply initialize objectD in A's constructor.)


[Jess in Action][AskingGoodQuestions]
prasad krishnan
Greenhorn

Joined: Dec 03, 2005
Posts: 5
Thanks for the early reply.But i am facing a problem . In my application This objectD is created with some parameters,which can be NULL. In methodB() i am passing NOT NULL constant values and in methodC() i always have NULL values. What i observe is that at times eventhough the user is calling methodB() they end up by getting NULL values in ObjectD. That is y i felt curious. FYI ObjectD is an ejb object
Steve Liem
Greenhorn

Joined: Dec 03, 2005
Posts: 17
If your class D has a default constructor,
then it will normally give you nothing.

D d = new D(); // declare an object D, and instantiate it to the constructor of D. (constructor without arguments) If you didn't defined a constructor in D, then it will automatically apply the default constructor:

public D() { }


Making 2 methods A and B where you instantiate and declare an object D, is just making 2 different objects D. So if you change some properties of D in say method B, it doesn't mean you've changed something in the object D in A, because it's a totally different object. (Your methods don't have a return type by the way. Or did you really wanted to write constructors?)
prasad krishnan
Greenhorn

Joined: Dec 03, 2005
Posts: 5
The code snippet is just for providing a picture of my scenario. I will get back on what exactly is bugging me.
prasad krishnan
Greenhorn

Joined: Dec 03, 2005
Posts: 5
I am back )). I have some additions to my code

Singleton Class A {

methodB(){
D ObjectD = new D(x,y,z,);// not a default constructor. I used it for simplicity

object F = cache(ObjectD);

}

methodC() {
D objectD = new D(x,y,z);

object F = cache(ObjectD);
}
}

public object cache(D objD){
Object G = objD.getX();
return G;
}
}

Now here if more than one user uses the method cache() with different values for ObjectD is there a chance of getting the value of X passed in methodB() to come when methodC() ids called.. our App is real huge with nearly 500 - 1000 using it regularly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton class methods
 
Similar Threads
setRollbackOnly() related question
synchronization basics
Accessing a superclass's method of and instance of outter class from static inner class
Help me to resolve this in Java
objects and references