Results 1 to 5 of 5
- 02-07-2011, 05:21 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Using variables from different classes within same package
Hello,
Although it sounds stupid....
I have a Java program in Eclipse that is using different classes and they all are within the same default package....
I have defined and calculated the a variable in class A....
When I use this variable in class D of the same package, it says:
myvariable cannot be resolved to a variable.....
Any idea, whats going wrong and how I can use that variable.....
Many Thanks...
Regards.
-
If it is a static variable, it must be accessed on the containing class. If it is a non-static class field, it must be accessed from an object of the containing class. If you need more help, you'll want to post sample code with code tags.
- 02-07-2011, 07:12 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
Could you let us know how you are using myvariable?
If myvariable is private or protected you cannot use it.
But if it is public or package-private you can get to it:
Java Code:class MyClass { public int myvariable = 5; } public class MainClass { public static void main(String[] args) { MyClass myClass = new MyClass(); System.out.println(myClass.myvariable); } }
- 02-07-2011, 07:36 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Hello,
Thanks very much.... It worked....
Regards
- 02-07-2011, 07:38 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
final variables in abstract classes
By parulmahajan in forum New To JavaReplies: 4Last Post: 06-11-2010, 09:54 AM -
Passing dynamic variables between classes
By Brekk in forum New To JavaReplies: 1Last Post: 03-22-2010, 06:07 AM -
sending variables to different classes?
By Erdrick in forum New To JavaReplies: 9Last Post: 08-04-2008, 11:11 AM -
get classes of a certain package
By ashraf shaban in forum New To JavaReplies: 4Last Post: 07-08-2008, 07:20 PM -
[SOLVED] Need help using variables between classes
By Renegade85 in forum New To JavaReplies: 4Last Post: 04-22-2008, 02:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks