RE: java: passing around class instances?

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 9 May 2010 14:59:40 -0400

Simply store that instance anywhere you like, then expose it with a getter.

The getter simplye returns that instance. You can initialize  it in the 
constructor or if it's static, simply do an if statement to
verrify it's not null, if it is null, istantiate it, then return it.

This is called a singleton design pattern.

Like this

public MyClass getStuff() {
if(stuff == null)
stuff = new MyClass();

return stuff;
}

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of mehgcap
Sent: Sunday, May 09, 2010 2:51 PM
To: programmingblind
Subject: java: passing around class instances?

Hi all,
I am working with Android (yet again). Because of the way Android works, you 
cannot have a dialog created that takes arguments from
the parent; instead, you use a data structure called an intent, in which you 
put everything the child needs to run. However, I have
a non-serializable object (intents only accept primitives, strings, and 
serialized objects) which the child needs to access but
which is created in the parent. I want to know how I can give two separate 
classes, one of which is the main class, access to the
same instance of an object? As a simple example, say we have object o, in class 
c.
Class a is the parent and has in it the line:
c obje=new c();

Now, class a's child, class b, needs access not just to class c, but to the 
specific obj instance of c that was made in a. Is there
a way to let both classes share the same instance of an object without 
serializing? I maybe could import a inside b, but a is the
main file (runs at application start) so I feel like that would not be a great 
way of doing things. Any ideas or thoughts would be
greatly appreciated. I can post the android code if you want, but I am not sure 
how much good it would do.

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap __________ View the list's 
information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: