Help me with static variables.

Put your problem here if it does not fit any of the other categories.

Help me with static variables.

Postby Jorenm » Wed Dec 09, 2009 1:55 am

Ok, so I have a class which extends a View. In that class I create a bunch of other classes and assign them to static variables in the View class.

Why do they register as null if I try to access them?

My problem is that if I have a bunch of classes I want to be able to access each other I have to pass references to every class to every other class. Is using static not the proper way to get around this?

Like, if I create classes 1 2 3 and 4. Class 4 creates class 5 and class 5 wants to access classes 1 2 3 and 4, how do I do that without passing a reference to it?
Jorenm
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Nov 28, 2009 9:16 am

Top

Postby nhenze » Thu Dec 10, 2009 11:04 am

Sounds like a very bad design. However, you might want to use the singleton pattern for some of your classes if you only need a single instance for each of them.
nhenze
Freshman
Freshman
 
Posts: 9
Joined: Thu Dec 10, 2009 10:34 am

Postby hardcoras » Thu Dec 10, 2009 5:25 pm

As nhenze sad, using static variables is a bad habit. Try to avoid it.
Singleton pattern also uses static variable, but there is no way around.

Use singleton object to hold your application context. And in this context put logically to the app context related objects hierarchy.

In non-embedded systems most programmers (at least I do) use IoC containers (like quice, spring, etc.). It is an alternative solution for a bigger applications but I think is too heavy for Android. But you can give it a try.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby Jorenm » Thu Dec 10, 2009 8:57 pm

Use singleton object to hold your application context. And in this context put logically to the app context related objects hierarchy.


Could you go into that a bit more? I don't think I understand what you're saying.
Jorenm
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Nov 28, 2009 9:16 am

Postby hardcoras » Thu Dec 10, 2009 9:11 pm

About Singleton design pattern: http://en.wikipedia.org/wiki/Singleton_pattern

About context i'll give you for example game engine:
Game engine is created as a singleton. To get the engine object i run GEngine.getInstance();
The engine contains GameView and Android Activity Context objects. So from anywhere I can get GameView or activity Context with a code: GEngine.getInstance().getGameView() or GEngine.getInstance().getContext().

This is just a simple example, I don't want to expand myself to much here. It's just a standard practice used all around.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby Jorenm » Fri Dec 11, 2009 2:48 am

Thanks, that helps a lot. I converted the classes that I only ever needed one of.
Jorenm
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Nov 28, 2009 9:16 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 16 guests