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
}
}
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);
}
}
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
}
}
Output
Cannot find symbol
class GlobalError2
{
static String name = "Scooby";
public static void main(String args[])
{
System.out.println(name);
}
}
3.Error: Using Variable Without Declaration
class GlobalError3
{
public static void main(String args[])
{
name = "Tom"; // ERROR
System.out.println(name);
}
}
Output
cannot find symbol variable name
class GlobalError3
{
static String name;
public static void main(String args[])
{
name = "Tom";
System.out.println(name);
}
}
4.Error: Missing Semicolon in Global Variable
class GlobalError4
{
static String name = "Scooby"
public static void main(String args[])
{
System.out.println(name);
}
}
Output
';' expected
class GlobalError4
{
static String name = "Scooby";
public static void main(String args[])
{
System.out.println(name);
}
}
5.Error: Using Local Variable Outside Method
class GlobalError5
{
public static void main(String args[])
{
int a = 10;
}
System.out.println(a); // ERROR
}
output
illegal start of type
class GlobalError5
{
static int a = 10;
public static void main(String args[])
{
System.out.println(a);
}
}
6.Error: Using Global Variable Before Object Creation
class GlobalError6
{
int price = 500;
public static void main(String args[])
{
System.out.println(price); // ERROR
}
}
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);
}
}
Top comments (0)