Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3508
  • Last Modified:

in as3 - public static variables make life easy so what's the problem?

I have many classes spread across many .as files under one package.
for example:

MyClass1.as
package MyPackage{
public class MyClass1{
public static var MyVariable:String
}
}

MyClass2.as
package MyPackage{
import MyPackage.*;
public class MyClass2{
 MyClass1.MyVariable ="Hello";
}
}

I've learned about setters and getters as well but why bother?  Does static take up more memory?  Is the code gonna run slower?  What's the down side?
0
gotaquestion
Asked:
gotaquestion
  • 2
  • 2
1 Solution
 
blue-genieCommented:
the premise is that keeping things private when they should be private is better for security reasons and also for preventing things being changed or accessed unintentionally.
if you have 2 classes that you use and they both have a public color variable - confusion could arise later on.
i don't think there is that much difference in terms of memory usage.
0
 
Jones911Commented:
static variables/classes are not garbage collected as they never fall out of scope.  They waste more memory then non static variables.

Getters/Setters arnt needed if your not doing anything tricky on the variable. ie

public function setData(data:String):void{
     _data = data;
}

This is an examples of a setter that really does nothing and isn't needed unless you like the idea of pure encapsulation and prefer to code this way.  There are arguments ( valid ) both for and against getters/setters like this but its down to personal preference.
0
 
blue-genieCommented:
just out of interest - any object is eligible for garbage collection when there is no reference to it. so your static variables/classes will be gc'd if there no more reference to the object to which it pertains?

0
 
Jones911Commented:
But if you create it once and put data in it it will never be out of scope anywhere in your application you can then reference that same item.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now