Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use static or getters/setters? [closed]

I am making a small musicplayer application in android , in which my main activity is using lots of static variables like this :-

 public class Fragmentactivity extends FragmentActivity {

         static ArrayList<String> s3=new ArrayList<String>();
        static ArrayList<String> s5=new ArrayList<String>();
        static ArrayList<String> songpaths=new ArrayList<String>();
         static ArrayList<String> alldirectorypaths=new ArrayList<String>();
         static ArrayList<String> internalpaths=new ArrayList<String>();
         static ArrayList<String> tempsongnames=new ArrayList<String>();
         static ArrayList<Bitmap> thumbnails=new ArrayList<Bitmap>();
         static int internal=0;
         static ArrayList<String> folderpaths=new ArrayList<String>();
    //my addtional code after it
    }

And I am accesing it in other classes like :-

  public class A{
    Fragmentactivity.songpaths.get(index);
    //and in  many other classes also i used it

I have made many static variables everywhere in my most of classes But now I learnt that this is not good practice for developing .

Should I continue with static or shoud I use getters/setters ?

Or if there are any other way to reuse variables...please suggests me.

Any help would be appreciated :)

Thanks in advance :)

like image 227
Animesh Mangla Avatar asked Nov 27 '22 04:11

Animesh Mangla


1 Answers

Am not an Android expert. static have a special meaning that they can be shared across all the instances.

Hence if you are going to share them across multiple instances of them, just goahead with current way.

Should i continue with static or should i use getters/setters ?

Assuming you want shared variables, I'll mix them both

Fragmentactivity.getSongNames().get(index);

Update :

If you are not making multiple istances of it, just get rid of static and provide getter and setters to it. That is all you need.

public class Fragmentactivity extends FragmentActivity {

     private ArrayList<String> s3=new ArrayList<String>();

   ...
   ...
    public ArrayList<String> getS3() {
    return this.s3;
   }

   public setS3(ArrayList<String> input){
   this.s3= input;
  }

...
like image 131
Suresh Atta Avatar answered Nov 29 '22 19:11

Suresh Atta