• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to pass values between classes?

 
Greenhorn
Posts: 23
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good responses. I just want to add that you control access to your class with the modifiers (coderanch, private, protected). Only public methods/variables can be seen by other classes. That's why the getter/setter methods are declared coderanch.
 
Kasun Liyanage
Greenhorn
Posts: 23
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all! Marc, that example solved my problem, thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic