• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing class variables from another class  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!