• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing class variables from another class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

1st post here so i hope someone can help
I need the syntax to access a variable from a class other than the one it was created in




for example if i need to use variableB from classB in classA what is the correct syntax to allow this
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there are a few ways to do it. if all you need is the value, you can write a getter method in class b. something like (and i'm winging this)



then, in your A class, you need to create an instance of classB, then call the method




again, this is a quick write, so it may need to be fine tuned.
 
jim cam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks fred that way does work OK , any idea how i could use it for multiple classes without instantiating new objects each time ? ( i know i should have been more specific in the 1st post )

The program im working on is a sudoku solver / generator which has several classes which need to access common variables

Thanks
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, here's the thing. if you are trying to access a member variable of an object, you have to have an instance of the variable for there to be a value. in other words, if you have not created an object of type B somewhere, there is no value for you to get.

Now, that doesn't mean you have to create the B object in your A object. you could have some setter methods that takes in the objects you want to refer to, for example.

there is also a design pattern, i forget the technical name, but it's the publish/subscribe pattern. each publisher object can register a bunch of subscriber object, an then when the value in the publisher changes, it notifies each subscriber.

you could also consider having some kind of aggregation/collection. Maybe your A object just needs a reference to an ArrayList, and then it can look at every object in there?

If you're trying to come up with a way to use global variables, then you're not really thinking OO. One usually tries to AVOID global variables in OO design.
 
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
Note: By convention, Java class names begin with an uppercase letter, and variable names begin with a lowercase letter. So before I confuse myself, I'm going to use "ClassA" rather than "classA" here.

In fred's illustration, ClassA HAS-A ClassB, which allows an instance of ClassB to be referenced within ClassA. That's the key. In his quick example, he created a new instance of ClassB within ClassA -- and I think this is the part you're questioning. To fine-tune fred's HAS-A approach, you could use a constructor to pass a reference to an existing ClassB into ClassA...

Or as fred just suggested above, you could use a setter method instead of a constructor.

Basically, you need to take a step back and look at your program as a whole -- considering how all the different pieces will need to interact. It might help to draw some diagrams of how everything relates, and based on that, decide where to create the instances, and where to pass the references.
[ December 23, 2006: Message edited by: marc weber ]
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic