java - Create a global instance of an object within Android Studio App -


i designing game in android studio. have activity multiple fragments on it. have player object want create instance of.

player player1 = new player(); 

all these fragmenets on activity making changes player1. instead of passing object each fragment , successive activity with:

intent.putextra("player",player1); 

is there way access player1 other fragments , activities without going through trouble of passing putextra() method?

i tried making player1 static:

static player player1 = new player(); 

but, couldnt access player1 other .java activity files.

so, basically, want strategy make player1 global object can reference whenever , wherever. don't want make methods within player class static, because want able have player1, player2, , player3 files on game, need able create instance of player, , don't think can done if every method static (right?)

i started oop, sure there's simple overlooking, or perhaps rather messing modifiers there better way achieve want. maybe using interface? how interface work?

what storing info in application object creating class extends application?

if want access anywhere, , don't care privacy, need make public

public static player player1 = new player(); 

the default package level access only.

you can access using syntax in other classes:

activity1.player1; // something... 

note: there better design alternatives making field public can access anywhere, that's bare minimum needed. it's not recommended in way, , since you're learning, might want read on of these design concepts.

usage of public getters/setters 1 of more common ways access field of class/instance, example.

private static player player1 = new player();  public static player getplayer1() { return player1; } public static void setplayer1(player p) { player1 = p; }  // ... 

so, basically, want strategy make player1 global object can reference whenever , wherever. don't want make methods within player class static, because want able have player1, player2, , player3 files on game, need able create instance of player, , i don't think can done if every method static (right?)

no, that's not true. classes static methods can instantiated long there public constructor and/or it's not abstract. static means it's accessed @ class level, , not require instance, though can still access static methods object reference. static means all instances share same fields, there 1 field, method, etc. across instances of class.

one design pattern might consider using singleton pattern, allows single instance of class whenever need it, don't need mess static properties , methods (which can have drawbacks if don't need use them).

what storing info in application object creating class extends application?

according documentation, acceptable means, though might still want think of overall design choices , determine if that's route want take.

base class need maintain global application state. can provide own implementation specifying name in androidmanifest.xml's tag, cause class instantiated when process application/package created.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -