• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables  RSS feed

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am i able to cross-referance variables from 2 seperate programs?

For example:
If program X sets int R at 120 can program Y have the statement R = R + 80
without actually initalizing int R in program Y?

Thank you for all your help in advance!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "two programs" do you really mean two different instances of the JVM launched separately by the OS, or do you just mean two different Java classes within the same program?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atrus, perhaps you could post an example, in code as best you'd guess it would work, of what you're trying to do.
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now the next Program would be:


These are two seperate programs. With these two simple programs I pretty sure that, that would now work. What I want to know is if there is anyway to access the "R" in the first program in the second program.
Oh, and sorry about any mistakes I whipped these two up pretty quickly.

Thanks for your time!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While not directly addressing your inquiry, I'd recommend reading the "Cup Size" and "Pass-by-Value Please" stories in the JavaRanch Campfire Stories.

One topic that surrounds your inquiry, is that of scope. Another is access. And another is good design. Also, I'd suggest that if you're not yet familiar with what an object is, what a class is, what a static context is, and you intend to program in Java beyond the current program you're asking about, you'd do well to become very familiar with these topics, sooner rather than later. Do feel free to ask more questions. If it's a question on a new topic, I'd recommend starting a new thread for its discussion.

Concerning scope: Note that the variable R declared inside the main method of VariR is only accessible inside the main method. Anything outside cannot touch it - or even know about it. It's a local variable - local to the method in which it was declared.

If you were to move R's declaration outside of any method, and make it a member of the class, then things outside the context of the main method could access it.

Note that if I've defined a static class member named R in the class VariR, depending on the declared accessibility (public, private, etc.) of R, it would be accessible from UseR, which would refer to it as VariR.R . However, doing this is typically not a good design decision.

I'd be glad to discuss the design considerations later, as I'm sure others would be, if you inquire about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!