DEV Community

Cover image for Common Errors While Using Global Variables in Java Programming
PRIYA K
PRIYA K

Posted on

Common Errors While Using Global Variables in Java Programming

1.Error: Accessing Instance Global Variable in Static Method

class GlobalError1
{
    int cost = 1000;   // instance global variable

    public static void main(String args[])
    {
        System.out.println(cost);   // ERROR
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
non-static variable cost cannot be referenced from a static context

class GlobalError1
{
    int cost = 1000;

    public static void main(String args[])
    {
        GlobalError1 obj = new GlobalError1();
        System.out.println(obj.cost);
    }
}
Enter fullscreen mode Exit fullscreen mode

2.Error: Using Object for Static Variable

class GlobalError2
{
    static String name = "Scooby";

    public static void main(String args[])
    {
        System.out.println(namee);   // spelling error
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
Cannot find symbol

class GlobalError2
{
    static String name = "Scooby";

    public static void main(String args[])
    {
        System.out.println(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

3.Error: Using Variable Without Declaration

class GlobalError3
{
    public static void main(String args[])
    {
        name = "Tom";   // ERROR
        System.out.println(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
cannot find symbol variable name

class GlobalError3
{
    static String name;

    public static void main(String args[])
    {
        name = "Tom";
        System.out.println(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

4.Error: Missing Semicolon in Global Variable

class GlobalError4
{
    static String name = "Scooby"

    public static void main(String args[])
    {
        System.out.println(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
';' expected

class GlobalError4
{
    static String name = "Scooby";

    public static void main(String args[])
    {
        System.out.println(name);
    }
}
Enter fullscreen mode Exit fullscreen mode

5.Error: Using Local Variable Outside Method

class GlobalError5
{
    public static void main(String args[])
    {
        int a = 10;
    }

    System.out.println(a);   // ERROR
}
Enter fullscreen mode Exit fullscreen mode

output
illegal start of type

class GlobalError5
{
    static int a = 10;

    public static void main(String args[])
    {
        System.out.println(a);
    }
}
Enter fullscreen mode Exit fullscreen mode

6.Error: Using Global Variable Before Object Creation

class GlobalError6
{
    int price = 500;

    public static void main(String args[])
    {
        System.out.println(price);   // ERROR
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
non-static variable price cannot be referenced from a static context

class GlobalError6
{
    int price = 500;

    public static void main(String args[])
    {
        GlobalError6 obj = new GlobalError6();
        System.out.println(obj.price);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)