Results 1 to 18 of 18
- 08-09-2010, 11:00 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
Call variables from one class to another
Hello!
I have made one Java library named Helper containing jar file with reusable classes. In it I have made one class (it does passing parameters using POST method to a php file). I am planning to call it every time when I do passing parameters. It has one method which has to use variables from my applet.
My problem now is with variables. How to recognize variables declared in my applet and to use them in that class and in it's method?
I would be grateful for any help.
- 08-09-2010, 11:13 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
I don't speak english that much but normally I do understand the post, in this case I didn't understand it at all.
so you made a class to pass parameters to a php script, when do you want to use it?
can you please write some pseudo code what you want to do?
what is exactly the problem because everything sounds kinda confusing
- 08-09-2010, 11:30 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
I pass parameters to a php file which then does recording of that parameters to database.
I was confused about passing variable values to class which does that, but I found a solution: I made variables in my class and declared them as "public static". Then I initialized them in applet by calling classname.variablename=value.
I hope that it is a good solution...?
- 08-09-2010, 11:54 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Not good.
Pass the values into the method as parameters.
Using public statics that can be changed from anywhere in the system is a Bad Thing.
- 08-09-2010, 12:08 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
Can you, please, give me an example for that?
It would help me a lot.
- 08-09-2010, 12:27 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 08-09-2010, 02:27 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
I have been reading this... Can you give me some simple example for my problem? It would help me more.
- 08-09-2010, 02:45 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That tutorial page has as good a set of examples as I can give...short of actually writing the answer to your problem, which I can't do since I don't know your problem.
- 08-09-2010, 02:49 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
Here is how I have just done:
I have one class PassParameters which has variable url and passToFile() method. In applet's class I made an object like this
and called variable and method like this:Java Code:PassParameters p = new PassParameters();
I just need you to tell me is it ok?Java Code:p.url = new URL("http://localhost/subtest.php"); p.passToFile();
- 08-09-2010, 03:07 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I have no idea what it is you are doing, but I will say this:
breaks OO encapsulation. You shouldn't assign directly to an objects attributes. There should generally be a setter for that. Or at least pass it into the constructor. But those decisions all depend on what it is you are doing.Java Code:p.url = new URL("http://localhost/subtest.php");
- 08-09-2010, 03:19 PM #11
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
I am trying to pass this url address from my applet to class PassParameters to use it.
How to assign this "http://localhost/subtest.php" to url variable?
If I writeI got error that it found java.lang.String but it was looking for java.net.URL.Java Code:p.url = "http://localhost/subtest.php";
- 08-09-2010, 03:24 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Give PassParameters a setURL(String url) method, which takes a String and turns that into a useful URL.
You clearly know how to create a URL since you did it earlier, so that is what that method would do.
- 08-09-2010, 03:37 PM #13
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
I did like this:
and called method from applet's class. Hope it is ok now.Java Code:public static URL setURL (String url){ try { adresa = new URL(url); } catch (MalformedURLException ex) { Logger.getLogger(PassParameters.class.getName()).log(Level.SEVERE, null, ex); } return adresa; }
- 08-09-2010, 04:15 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That wasn't what I was expecting.
Do you understand what I mean by an attribute?
"url" is (presumably) an attribute of a PassParameters object, ie:
and you use a setUrl() method to set it...which won't be a static method since this is not a static thing.Java Code:public class PassParameters { private URL url; // Rest of class goes here. }
- 08-09-2010, 06:17 PM #15
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
If you want to call variables from on class to another, just use reflection.
import java.lang.reflect.*;
- 08-09-2010, 06:19 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
-
- 08-10-2010, 08:37 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
how do i call a class from main class
By revanger in forum New To JavaReplies: 7Last Post: 07-20-2010, 05:32 PM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
getting class to recognize variables from another class
By shadycharacter in forum New To JavaReplies: 1Last Post: 04-26-2010, 10:14 PM -
How can I call abstract class methods from another class
By srinivas2828 in forum New To JavaReplies: 13Last Post: 03-12-2010, 02:33 PM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks