Results 1 to 4 of 4
  1. #1
    Kodate is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Passing Non-Static Variables to Other Classes

    Hello everyone!

    I've been stumped by this for a while, and no amount of googling or anything else can solve my problem. Let's say my code is as follows.

    Java Code:
    public class MyClass
    {
        int x = 1;
        public int getX()
        {
            return x;
        }
    }
    and
    Java Code:
    public class MySecondClass
    {
        int secondx = getX();
    }
    The problem with this is that I get the error that I can't reference a static context from a non - static.

    Please help!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Passing Non-Static Variables to Other Classes

    You can always pass values between instances of classes via method calls. What specifically are you trying to do and why? Also, you can always access a static from a non-static context assuming it is visible. However, you can't access an instance (non-static) variable from a static context.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Passing Non-Static Variables to Other Classes

    Those classes have nothing to do with each other (e.g. one doesn't extend the other) and your second class doesn't have a getX() method ... I don't see any static context either.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Passing Non-Static Variables to Other Classes

    Variants of this question get asked a lot. I'd really like to know what book or teacher is failing to teach such a fundamental concept.

    Because getX() is a non-static method, you need an instance of MyClass to call it on:

    Java Code:
    public class MySecondClass
    {
        MyClass mc = new MyClass();
        int secondx = mc.getX();
    }
    If it were a static method, you would call it like this:

    Java Code:
    public class MySecondClass
    {
        int secondx = MyClass.getX();
    }
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 3
    Last Post: 12-27-2011, 05:17 AM
  2. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 01:46 AM
  3. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 07:07 AM
  4. Replies: 6
    Last Post: 02-15-2010, 05:31 AM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 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
  •