• Post Reply Bookmark Topic Watch Topic
  • New Topic

declaration  RSS feed

 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following line of code
"private ArrayList observers = new ArrayList()"
My question is,how do make "observers" as a variable.? and add or remove objects from that variable.
Any help would be appreciated
Nash
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
observers is a variable.. ok it's reference to object actually and you can call methods like
observers.add("1 observer");
observers.add("2 observer");
If you came from C++ world... In Java you can't have object by value! You can have primitive types by value. In C++ you either have ptr, reference, or object by value. In Java only references exist.
By value you can have only primitive types. Like int.
int a = 5; // this is a value!
MyObject a = new MyObject(); // this a is reference to Object!
//You can't have object by value.
I hope that explains, best regards!
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate for the reply,I did understand a little,
But need to dig deeper......

Nash
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyObject aa; // this is definition! You define the reference;
MyObject bb = null; // you define bb reference and assign null to it!
// you create MyObject and make your aa reference to point it
aa = new MyObject();
MyObject aa = new MyObject();
equals
MyObject aa;
aa = new MyObject();
Now when you have the reference to object you can do with that Object whatever you want:
aa.mymethod();
aa.myfield = 5; // assign value to member variable.
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mate,thanks for the insight,
needs digging will dig
Nash
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!