Results 1 to 5 of 5
  1. #1
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

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

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

    Default

    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.

  3. #3
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    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);
    	}
    }

  4. #4
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Hello,

    Thanks very much.... It worked....


    Regards

  5. #5
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Actually

    What I did... I passed myvariable as parameters in the method header where I am using it....

    So now it is working well..

    Thanks for cooperations...

Similar Threads

  1. final variables in abstract classes
    By parulmahajan in forum New To Java
    Replies: 4
    Last Post: 06-11-2010, 10:54 AM
  2. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 07:07 AM
  3. sending variables to different classes?
    By Erdrick in forum New To Java
    Replies: 9
    Last Post: 08-04-2008, 12:11 PM
  4. get classes of a certain package
    By ashraf shaban in forum New To Java
    Replies: 4
    Last Post: 07-08-2008, 08:20 PM
  5. [SOLVED] Need help using variables between classes
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 03:53 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
  •