Results 1 to 7 of 7
  1. #1
    a45b22chp is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default [SOLVED] Simple Q: Values between Classes

    I searched through the posts for a bit but couldn't find the answer so...


    I have 2 classes within one package. I want to access a value from class A in class B. I only need to do it once, and class A never even needs to act on the value once it is instantiated except to pass it to B.

    Java Code:
    class TCPServer {
    public static void main(String argv[]) throws Exception
    {
    [COLOR="Red"]double X = 15[/COLOR];
    }


    Java Code:
    class TCPClient {
    public static void main(String argv[]) throws Exception
    {
    
    double Y = [B][COLOR="Red"]X[/COLOR][/B];
    }


    thanks in advance!
    Last edited by a45b22chp; 04-25-2008 at 05:21 PM.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    it is the same on how you access methods in other classes in the same package....

    double b = new OtherClass().doubleVar;
    freedom exists in the world of ideas

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    If you declare the variables in a method (you declared them in main) then they are local to that method and cannot be seen anywhere else in the code (with some exceptions - see Local Inner Classes | Little Tutorials)

    Your code should look like this:
    Java Code:
    class A
    {
        public static double sX;
        public double X;
    }
    
    class B
    {
        public static void main(String[] args)
        {
            double Y = new A().X;
            // or
            double Z = A.sX;
        }
    }
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #4
    a45b22chp is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Each class is in it's own file.

    Eclipse says:
    "_____ cannot be resolved or is not a field"

    any thoughts?

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    Make 2 class files,

    1 called A and one called B.

    class A:
    Java Code:
    public class A {
    	
    	public static double Ax()
    	{
    		double X = 15;
    		return X;
    	}
    
    }
    class B:
    Java Code:
    public class B {
    
    	public static void main(String[] args) {
    		
    		A a = new A();
    		double Y = a.Ax();
    		System.out.println(Y);
    	}
    
    }
    Output:

    Java Code:
    15.0
    I've just ran this in Eclipse and it works fine.
    Did this post help you? Please me! :cool:

  6. #6
    a45b22chp is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    thanks DonCash, for the help and the quick response


    just curious, how would class B call 2 different values from A? would class A need to define more than one method, and each method can return one value?

  7. #7
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    No problem. Yeah just add some more methods to class A and call them the same way in B.

    You only need to create the A object once.

    Java Code:
    A a = new A();
    Did this post help you? Please me! :cool:

Similar Threads

  1. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 02:31 PM
  2. Help with classes
    By freswood in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 03:28 PM
  3. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  4. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 07:08 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 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
  •