Results 1 to 7 of 7
- 04-25-2008, 11:56 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
[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.
- 04-25-2008, 12:56 PM #2
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
- 04-25-2008, 04:08 PM #3
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
- 04-25-2008, 05:23 PM #4
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
Each class is in it's own file.
Eclipse says:
"_____ cannot be resolved or is not a field"
any thoughts?
- 04-25-2008, 05:40 PM #5
Make 2 class files,
1 called A and one called B.
class A:
class B:Java Code:public class A { public static double Ax() { double X = 15; return X; } }
Output:Java Code:public class B { public static void main(String[] args) { A a = new A(); double Y = a.Ax(); System.out.println(Y); } }
I've just ran this in Eclipse and it works fine.Java Code:15.0
Did this post help you? Please
me! :cool:
- 04-25-2008, 05:49 PM #6
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
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?
- 04-25-2008, 05:55 PM #7
Similar Threads
-
Cant run my classes
By Assaf A in forum EclipseReplies: 1Last Post: 04-22-2008, 02:31 PM -
Help with classes
By freswood in forum New To JavaReplies: 5Last Post: 04-21-2008, 03:28 PM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 07:08 PM -
When do we use inner classes?
By cruxblack in forum New To JavaReplies: 5Last Post: 08-10-2007, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks