Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass values between classes?  RSS feed

 
Kasun Liyanage
Greenhorn
Posts: 23
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean, if one class is already initialized and if i want to access a variable (which is initialized with a value) in that class from another class, how should i do it?

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Liyanage wrote:i mean, if one class is already initialized and if i want to access a variable in that class from another class...

If you're really talking about classes (that is, static variables), then simply use the class name. For example, MyClass.x or MyClass.y.

But if you're talking about instances of these classes, then you need a reference to that instance. For example...

Note that in the above example, a constructor ensures that each instance of ClassB HAS-A specific instance of ClassA. Then within that instance of ClassB, you can use myA.x to access the variable. (Assuming there are no access restrictions.)
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc is quite right; I'm going to elaborate a bit on an explanation that doesn't involve static variables, on the theory that it was not what you were asking about. If it was, his explanation might do fine.

A class definition just defines what an object will look like. So:

just defines what an object of type Auto will have in it. In order to actually have an object of this type, you can create one with:

But there is no definition in this tiny class for setting or getting the value of its color. So the original definition might really should have been:

And now code can create an object of this type, set its color, and get its color with things like:


So that's another answer to your question. This example keeps to the principle of "hiding" the actual variable within the class, so that the only way to change it or access it is by using its 'setter' or 'getter' methods. There are other ways to do this, including a way to allow the color to be accessed with syntax like "familyCar.color", which could not be done without a change to the class as I have it.

rc
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good responses. I just want to add that you control access to your class with the modifiers (public, private, protected). Only public methods/variables can be seen by other classes. That's why the getter/setter methods are declared public.
 
Kasun Liyanage
Greenhorn
Posts: 23
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all! Marc, that example solved my problem, thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!