This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How i can set a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How i can set a variable " Watch "How i can set a variable " New topic

How i can set a variable


Joined: Oct 08, 2002
Posts: 5
How i can set a variable
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Something like that?

Please ignore post, I have no idea what I am talking about.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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
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?

"JavaRanch, where the deer and the Certified play" - David O'Meara
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

wood burning stoves
subject: How i can set a variable
jQuery in Action, 3rd edition