Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • Re: Common Java Mistakes

    Problem description: Referencing Non-static variables/methods in a static way
    Problem category: Compile-time Problems

    Diagnosis Difficulty: Easy
    Difficulty to Fix: Easy-medium


    This problem occurs when you try to access an instance variable in a static way. The most common way this happens is to try to use an instance variable from a static method (such as the main method) without creating an instance of that object. For more information on static and instance variables/methods, see Understanding Instance and Class Members.

    public class Point
    {
        public int    x, y;
     
        public static void main(String[] args)
        {
            x = 5;
            y = 3;
        }
    }

    The second common mistake is to forget to declare your static variables as static (the default is as an instance variable)

    Public class Test
    {
        public int count;
     
        public static void main(String[] args)
        {
            count = 0;
            while(count < 5)
            {
                ++count;
            }
        }
    }

    This problem can also manifest itself by trying to call a non-static method.

    public class Test
    {
        public void doIt()
        {
            System.out.println("Hello world!");
        }
     
        public static void main(String[] args)
        {
            doIt();
        }
    }

    Error Messages

    The error message for this error is very common, and easily indicates what the actual problem is.

    Test.java:7: non-static variable a cannot be referenced from a static context
    a = 5;
    Test.java:10: non-static method doIt() cannot be referenced from a static context
    doIt();
    Suggested fixes

    Depending on your situation, you will either want to change the variable/method declaration to be static, or you will want to create a new instance of the object and reference the field/method from that object.

    public class Point
    {
        public int    x, y;
     
        public static void main(String[] args)
        {
            Point p = new Point();
            p.x = 5;
            p.y = 3;
        }
    }

    Public class Test
    {
        public static int count;
     
        public static void main(String[] args)
        {
            count = 0;
            while(count < 5)
            {
                ++count;
            }
        }
    }
    This article was originally published in forum thread: Common Java Mistakes started by helloworld922 View original post