Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton class methods

 
prasad krishnan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
prasad krishnan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic