• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Accessing variables in foreign class ...  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you have a method in ClassA which returns a number based on a private variable, e.g.:
//The Number variable
private int myNumber;
// skip code ...
//The method
public void returnMyNumber(){
return myNumber;
}
How would you go about accessing the number variable from ClassB? Something like:
// Return classA variable
public void returnClassAVariable(){
return ClassA.myNumber;
}
Any ideas?
 
Jack Weavesham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& thinking about it, how could you test that the ClassA myNumber variable equals a ClassB variable?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jack Weavesham:
Say you have a method in ClassA which returns a number based on a private variable, e.g.:
//The Number variable
private int myNumber;
// skip code ...
//The method
public void returnMyNumber(){
return myNumber;
}
How would you go about accessing the number variable from ClassB? Something like:
// Return classA variable
public void returnClassAVariable(){
return ClassA.myNumber;
}
Any ideas?


As ClassA.myNumber is an *instance* variable, you can't access it without having an instance of ClassA. And once you had, you'd just use the getter method:

Notice though that this (especially the naming) seems to suggest a design problem: a classes interface should generally be about its behaviour, not about its internal data.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jack Weavesham:
& thinking about it, how could you test that the ClassA myNumber variable equals a ClassB variable?


Can you please give a more complete example of what you'd want to do - and why? Thanks!
 
Jack Weavesham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is just a sample class structure to learn from. As an overall idea : how could you add a private variable in ClassA (for example) to a private variable in ClassB?
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what you are wondering Jack, is how to assign what is returned to a variable in Class B, and somehow add that to a private variable, as an excersize?
hmm interesting
ps - i know it's just an excersize, but in terms of the tenets of OO programming and encapsulation, i think private variables are meant to not be shared around? of course, it would be possible. just an idea.
[ January 11, 2003: Message edited by: Jasper Vader ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!