• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a variable from another class  RSS feed

 
Eline Øfstbø
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've googled this like 100 times and i just cant figure it out.








I have this Valuta Class, and i want to use the variable "usdollars" in this Machine class in ExchangeDollar method. I tried a lot of different things, but i just removed it all because nothing seems to be working.
 
Andrew Great
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it's something like this...





 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
want to use the variable "usdollars" 

Did you spell that variable name correctly? I can not find it in the posted code.

To get a value from an instance of a class, there needs to be a reference to the instance that can be used to call the class's get method"
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you
I think it is usvaluta, but that is an incorrect format for a Java® identifier. It shou‍ld read usValuta with a capital V to mark the second word. Similarly britiskValuta.
You are both wrong about access to variables. All variables inside all classes (fields) shou‍ld have private access. They shou‍ld be accessible (if at all) via getXXX and setXXX methods, as Norm has already told you.
Get rid of the sample method, which BlueJ has added just to confuse you. I would suggest you get rid of BlueJ too; I have used BlueJ and the scars have just about healed .
What is b supposed to mean; you cannot tell from its name?
 
Eline Øfstbø
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the answers 
Doesn't seem like my brain wants to understand this, so it would be great if someone could just change my code and write it correctly or just tell me which lines i have to change and what to write. Btw i cant use anything other than BlueJ because this is for school   
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eline Øfstbø wrote:. . . . Btw i cant use anything other than BlueJ because this is for school   
You will have to take the rough with the smooth, then.It is all right to return an int, which is a primitive, or a String, which is immutable, from a getXXX method. Some types would require copying before returning.
 
Eline Øfstbø
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how do i return/access "i" or "text" in another class?
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's has been answered in the posts above, but I will reiterate.  Get an instance (an object) of the class you want to get the value from.  Then call the method or access the value you want from that instance.  So in the above case:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!