• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How i can set a variable

 
Joig
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
How i can set a variable
Thanks
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joig,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Something like that?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joig:
How i can set a variable


Depends on where you want to set it FROM. When you are inside the code of a class, and the variable is a class member, you can just set it as shown above.
If you are in another class and you want to set the value of a member variable for an instance that you hold a reference to you can use:
myVarHoldingYourClass.IamTheKing = 27;
Well, this is true if the variable is accessible. If it is private (like it probably SHOULD be) then you have to rely on the class having methods to set the value. The class holding the private variable would probable have a method named
setIamTheKing(int i);
and you would call it using
myVarHoldingYourClass.setIamTheKing(27);
Now if the variable happens to be static then it does not relate to any particular instance of the class, so you can just use the ClassName to get at the public variable:
YourClass.IamTheKing = 27;
Do we need to continue and talk about local variables, and using this to refer to the instance that we are in?
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic