GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Passing a variable from one class to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing a variable from one class to another" Watch "Passing a variable from one class to another" New topic
Author

Passing a variable from one class to another

Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

Hi

I have a string variable which is in Package A and within a class where a single instance is created.
I need to get that variable to Package B to another class

I really don't want to pass the variable of that class instance across through the 15 or so classes and various packaged there are to get it to the needed destination.

Basically I need a way to say to my class that in Package A you'll find this class and this instance which WILL exist and get all its properties.

I am sure that their is an easy way to do this but unsure.

Thanks
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

If the String you are trying to get is a constant and can be declared something like this:



then you can use:




If you want to be able to read the string value, say from a file or when you start the program, you could use the Singleton pattern to give your other classes access to the value.

But then again, Singeltons are EVIL!!

So, another solution would be to use an interface that holds your static variables and have all the classes that needs the variable implement it. But that could be considered code obfuscation and that is even more EVIL!!!
Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

The variable is not a constant and is produced when the class instance is created!

Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

@Matthew

As long as you are able to figure out the value of this variable in the constructor of your class, it could still be made 'final', and hence, safe to be made 'public'.

Consider this:

And, in some other class, referring to this 'final' property via instance:

In your case you want to create only one instance (above example has two of them).

Hope this helps!
- Aditya
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Aditya Jha wrote:in some other class, referring to this 'final' property via instance:

In your case you want to create only one instance (above example has two of them).


some other class? you are setting and getting values at the same place . as you have mentioned earlier, how will you get the value in *some other* class?

<edit>
say, you are instantiate an object(let sat reference obj refers this) with value in class A. now have other class get the obj reference ?

note: exclude inheritance(IS-A)

</edit>
Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

Basically my problem looks like this

All in Package A


THen the class is initialised



Then over in packet B




Obviously this is a brief explanation and I don't want to do a simple println, however, This pretty much is the problem and it may have been answered above but if so I didn't understand the explanation.

Thanks for everyones input so far is really appreciated


ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

Before you start please go through the topics on access levels and concepts of 'static'.

If you want to make a variable available throughout all packages , you will need to make it public.

Then decide, how do you want that variable to be accessed in other classes. Do you want to access it directly or through and getter method. As per your code it looks like you want to access the variable using "Class-name dot variable-name" style. So in that case you will need to make the variable static so that you can access it using directly the class name.

Last , you will need to import the class in package from where you need to access that variable. In your case you will need to import the class in _Destination class in Package B.

I have tried to explain it using your code, but please make an habit to follow the standard rules for giving proper names to your classes and variables. Also you have tried using constructor to initialize a static variable, which is not correct, as constructors are used for initializing instance variables and not static variables,so in your case the static variable will not be initialize as long as you don't create any instance of that class.

Anyways, here is the code to achieve your objective. Please try it yourself before going through the code :

Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

@ashwin

I have sorted the problem now after I came up with a genius idea (if I say so myself), however, Your advice to go read more about static was the best advice I have had after I learnt a bit more about what static had to offer.

Cheers Code Ranch. All the advice has been great and I am sure to return sometime soon in the future

Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Matthew Auburn wrote:@ashwin

I have sorted the problem now after I came up with a genius idea (if I say so myself), however, Your advice to go read more about static was the best advice I have had after I learnt a bit more about what static had to offer.

Cheers Code Ranch. All the advice has been great and I am sure to return sometime soon in the future



Another small hint: Read up on Java Coding Conventions. Makes it easier to write code that is easily readable for other java programmers. (See http://www.oracle.com/technetwork/java/codeconvtoc-136057.html).
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Seetharaman Venkatasamy wrote:
Aditya Jha wrote:in some other class, referring to this 'final' property via instance:

In your case you want to create only one instance (above example has two of them).


some other class? you are setting and getting values at the same place . as you have mentioned earlier, how will you get the value in *some other* class?

<edit>
say, you are instantiate an object(let sat reference obj refers this) with value in class A. now have other class get the obj reference ?

note: exclude inheritance(IS-A)

</edit>


By passing the reference to the 'other' class in question.

The intent of my code piece was just to show that instance-specific final members are indeed possible, and can be given separate values upon construction of the instance.

As for the original issue, you can solve it with a static member. But if you do need an instance for some reason, then that is also possible by applying the code I had mentioned.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing a variable from one class to another