• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing variables to different classes  RSS feed

 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably going to be too easy for you pros, but for a beginner like me, it's a fight.

I have a class (called TFfView.java) that displays a rtf file in a JEditorPane. It works great, and I want to reuse it in a couple of other classes in a much larger program that are currently using a different procedure to output info. To make that work, I would need to pass a couple of variables. An int x and int y variable for the window size, a frame title(variable: String title), and the actual file.rtf name(variable:String file).

So, with that in mind, let's pretend that I am working in a different file called Class1.java. I run a decision-making routine, and decide to run a file: "example1.rtf", title: "example title", x=400, and y=600.


How would I:
a. Write the statement in my fictitious Class1.java to send those variable to the below posted RTFView.java file.
b. Rewrite RTFView.java to accept those incoming variables. main() would be replaced with start().


 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a method.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what that means.

I understand how to pass variables between methods within the same class. I don't understand how to do it between different classes. The receiver is the code I posted. The caller hasn't been written yet.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.
Inside your Class1 code you have a reference to an RTFView object. Maybe you created a new object in that code, or maybe it was passed in by some other code. You call the method of that object. Like this:


This is how an object (not a class) passes a variable to another object (not a class).
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a fictitious Class1 object.



Now, I want to transport those 4 variables to the rewritten RTFView. I've set up line 9 to receive the variables that somehow get sent here. Will those variables be "in scope" all the way through the execution of the below code?



So, now where do I go, or am I just way off to begin with?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Steven wrote:
Now, I want to transport those 4 variables to the rewritten RTFView. I've set up line 9 to receive the variables that somehow get sent here. Will those variables be "in scope" all the way through the execution of the below code?


Method parameters are local variables, and as such, they exist only during that particular invocation of that method. If you want to pass them to one method, and then have those values available later for another method in the same class, then you need to copy them to member variables.



Look for a section on local vs. member variables in any Java text or tutorial.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll take a look at that tutorial.

I'm going to pass this off to several other help sites as I'm still have no idea how to get the information for the one object to the other. I know there is a way and maybe someone on the other boards may have ran into this. Thanks anyways.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Steven wrote:Here is a fictitious Class1 object...

So, now where do I go, or am I just way off to begin with?


Sort of way off. If that's what you want to do, then you don't need a new method in RTFView. (Especially not a static one.) You need those variables in the RTFView constructor, so pass them in as parameters to that constructor. Right now they are local variables in the constructor, so change that so they are parameters passed in.

And then your Class1 object should create an RTFView object with those parameters, like this:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Steven wrote:I'm still have no idea how to get the information for the one object to the other.


Didn't we just show you that? Paul showed you how to pass the information as parameters, and you seemed to grasp that part of it, and I showed you how to put those parameters into member variables, to become part of the object's state (your RTFView's state) for later use.

If this bit is so confusing, then you really ought to forget this program for a minute, step back, and write a few simple programs that do nothing other than test your understanding of these basic concepts. Otherwise you're just setting yourself up for frustration.

I know there is a way and maybe someone on the other boards may have ran into this. Thanks anyways.


I promise you, the problem isn't that people here don't know how to do what you're asking about.
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hit resoled by accident when looking to hit reply.

That is exactly what I was looking to do. It worked perfect with some tweeking of my previous code.

Thank you, thank you, thank you.

Is this what they call overloading a constructor? What are some other help files that I might want to read. This is going to make my program so much easier to work with.

I'm so excited right now. This opens up a world of possibilities for my program!
 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff Verdegan

That is not what I was insinuating. I find it helpful quite often if I'm not quite being understood (and it's probably due to my phrasing), to throw it out in front of a whole group of people. I've been trying to teach myself programming for the last several weeks in my spare time as a hobby. I choose Java as it seems to be the most universal. So, my phrasing or thought patterns are not going to be the same as someone who's in school, or has formal training. I know what I want to do. I don't know how to express it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scotty Steven wrote:
Is this what they call overloading a constructor?


Overloading is when you have a method or constructor with the same name, but different parameter lists.



Here the constructor is overloaded, because there are two different signatures, and the printName() method is overloaded, because it has two different signatures.


What are some other help files that I might want to read.


"Help files"? I don't know what you're referring to.

You'll want an intro book such as Head First Java, or Bloch's or Eckel's intro books (one of which is available for free online, I think), and/or the Oracle tutorial at http://docs.oracle.com/javase/tutorial/java/, and you'll definitely want to keep the API docs handy: http://docs.oracle.com/javase/6/docs/api/

 
Scotty Steven
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant tutorials, not help files.

I actually find the oracle site to be some pretty hard reading. I've been trying to put this together with youtube vids mostly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!