Class not going static but others are [Solved]


(Lozza JP) #1

Hi having a few problems with static keyword, and also some clarification of use would be nice.

We have a DataHelper (saving and loading data) and ItemMenu (will display a menu when a button is pressed) class set as static, because they will not ever be instantiated, is that a correct way to think of it?

Or will ItemMenu be instantiated just the once and therefore should be not static?

They both are very empty classes and will be filled later, but the problem right now is we have an Assets class that contains a bunch of [embed] tags filled with the images, .oel levels etc. My understanding again is that Assets will not be instantiated as an object, merely it is like a container for all these? However it will not compile with Assets being a static class.

Error: The static attribute may be used only on definitions inside a class.

Tried to make my post generic as possible, not sure if the code snips will help. Literally all that is in the Assets class is [Embed]'s

Thanks for any guidance


(Jacob Albano) #2

Unlike in C#, you can’t mark classes in as3 as static. You can have static properties on a class, but use them sparingly. Searching for “static” on this forum will bring up numerous problems from just the past few weeks that are all caused by improper usage of global variables.

DataHelper is a class that I would personally make static, so you can access it from anywhere and not have to worry about creating duplicates. ItemMenu, on the other hand, I would not, assuming it’s an entity class.


(Lozza JP) #3

I don’t get what you mean you cant mark classes as static but you would make DataHelper static??

After some thought yes ItemMenu would be best as an entity, but there will only ever be one I guess, but it still needs to be instantiated right?(so no static here).


(Jacob Albano) #4

I meant that I would make all the values and functions on the class static.


(Lozza JP) #5

I have DataHelper marked as static though

public static class DataHelper

and the few variables in there are static.

Assets on the other hand gives a compile error when static class.

So I shouldn’t mark it as static as I have in the class dec?


(Jacob Albano) #6

Is it possible you aren’t referencing the DataHelper class yet? If you don’t use it in your code it won’t compile that file, so you won’t get a compile error. As far as I know, that’s not valid syntax.


(Lozza JP) #7

Oh that would make sense. Will test it later and remove static keyword in class declarations.

Thanks for that I’ve come predominately from Java background.

So if a data class acts as static by making all the variables static at least only one copy of variables will be stored?


(Martí Angelats i Ribera) #8

In AS3 there isn’t a static class (unlike in other lenguages). To create that you simply have to create a class (without the static) and make every variable and function static (and yes, the static variables will only be saved once).


(Lozza JP) #9

Thanks for your help guys!