Results 1 to 18 of 18
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default 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.

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    6

    Default

    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

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    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...?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    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.

  5. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Can you, please, give me an example for that?

    It would help me a lot.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

  7. #7
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I have been reading this... Can you give me some simple example for my problem? It would help me more.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    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.

  9. #9
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    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
    Java Code:
        PassParameters p = new PassParameters();
    and called variable and method like this:
    Java Code:
    p.url = new URL("http://localhost/subtest.php");
                       p.passToFile();
    I just need you to tell me is it ok?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    I have no idea what it is you are doing, but I will say this:
    Java Code:
    p.url = new URL("http://localhost/subtest.php");
    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.

  11. #11
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    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 write
    Java Code:
    p.url = "http://localhost/subtest.php";
    I got error that it found java.lang.String but it was looking for java.net.URL.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    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.

  13. #13
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I did like this:
    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;
            }
    and called method from applet's class. Hope it is ok now.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    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:
    Java Code:
    public class PassParameters {
        private URL url;
    // Rest of class goes here.
    }
    and you use a setUrl() method to set it...which won't be a static method since this is not a static thing.

  15. #15
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    If you want to call variables from on class to another, just use reflection.
    import java.lang.reflect.*;

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    If you want to call variables from on class to another, just use reflection.
    import java.lang.reflect.*;
    Yikes, what's wrong with passing object A to an object B if a B wants to know something from an object A?

    kind regards,

    Jos

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by crikey View Post
    If you want to call variables from on class to another, just use reflection.
    import java.lang.reflect.*;
    I agree with Jos, this is very bad advice indeed. Reflection should be used very sparingly and only with great fore-thought, and this is certainly not the place to be using it.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Some people seem to see reflection as the answer to everything...must be a vanity thing.
    ;)

Similar Threads

  1. how do i call a class from main class
    By revanger in forum New To Java
    Replies: 7
    Last Post: 07-20-2010, 06:32 PM
  2. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  3. getting class to recognize variables from another class
    By shadycharacter in forum New To Java
    Replies: 1
    Last Post: 04-26-2010, 11:14 PM
  4. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 03:33 PM
  5. Replies: 1
    Last Post: 01-03-2010, 10:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •