Results 1 to 10 of 10
  1. #1
    Erdrick is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default sending variables to different classes?

    hi,

    im having trouble sending int h from class A and int i from class B to class C where i'd like to multiply them.

    like,

    public class A {
    int h = 3;
    public sendVariable1() {
    return h;
    }}

    public class B {
    int i = 4;
    public sendVariable2() {
    return i;
    }}

    public class C {
    A one = new A();
    first = one.sendVariable1();

    B two = new B();
    second = two.sendVariable2();

    int total = first x second;
    }

    hoping for total = 12, the problem seems to be that even though i've initialized and used the integers h and i outside those send methods, they are not being found... im pretty confused, i know this is really basic. thanks for any help.

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    What result are you seeing? Your functions need to be declared to return a value. What you have won't compile.

    Also: you really should use different names. You are not "sending" the variables, you are "getting" them. So better function names would be "getVariableA()"

    which should be declared something like:

    Java Code:
    public int getVariableH() { return h;};

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Better read some related docs about method declarations and stuff. Method signature, retuning values and so on.

  4. #4
    Erdrick is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    sorry about the confusing names, i was trying to express an idea and wasn't using real code. it wont run because the variable h isn't being found in the method in class A somehow.

    it would look more like this then:

    public class A {
    public static void main (String[] args) {int h = 3;}
    public int getVariableH () {return h;}
    }

    and then in class C...

    public class C {
    A object = new A();
    System.out.println(object.getVariableH());
    }

    im guessing i just cant have extra methods in a class that already has a main one, with the public static void main stuff? it gives me a "cant find h" error, and if i place the getVariableH inside the main one it gives me an "invalid start" error. is that it?

    some links would be nice.

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Standard code would look like:

    Java Code:
    public class A {
    int h = 3;
    public static void main(String[] args) {
         C aC = new C();
    };
    public int getVariableH () {return h;}
    }
    
    
    public class C {
    A object = new A();
    System.out.println(object.getVariableH());
    }

    you then test it by executing the main in A

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Hi fishtoprecords, you have a simple mistake at line 5. Additional colon is there. I know you just right the code here without testing. But it's much better to double check before click the post button for code segments. ;)

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You are correct, I wrote it in the forum, not a real compiler.

    But what colon? there are no colons in my code. There may be an extra semicolon, but they are nearly always ignored by the compiler.

    There is nothing wrong with writing
    int h =3;;;;;
    the compiler just throws the extra ones away.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ooops, I'm talking about semicolon. Sorry about that.

    Just look at the main method in line file. Can you find this?

    Java Code:
    };
    You right, compiler it neglect simply. If you have found such a thing inside a code, then you get a warning about an empty statement. That's why I said it's a mistake, not a compilation error. Reason is to name it as a mistake, newbie can think that it's a part of Java statement.

  9. #9
    newmember is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    public class A {
    public static void main (String[] args) {int h = 3;}
    public int getVariableH () {return h;}
    }
    
    and then in class C...
    
    public class C {
    A object = new A();
    System.out.println(object.getVariableH());
    }
    If this is the real code. then i think you have defined "h" in main method. which is a local variable and not the instance. getVariableH() will not have access to the local variable of main().

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes newmember, if you want to use a same variable in two method then you have to define them in a common place. That means, define them globally.

    So our thread stater has lots of feedback here. Still no response :)

Similar Threads

  1. sending SMS between PC and mobile
    By Halayas in forum Sun Java Wireless Toolkit
    Replies: 6
    Last Post: 03-18-2010, 11:45 AM
  2. Sending a Mail
    By haiforhussain in forum Advanced Java
    Replies: 7
    Last Post: 07-30-2008, 12:28 AM
  3. [SOLVED] Need help using variables between classes
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 02:53 PM
  4. Sending out UDP pockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:07 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
  •