Menu

Common Errors While Using Global Variables in Java Programming
🖼️

Common Errors While Using Global Variables in Java Programming

DEV Community·PRIYA K·6 months ago
#O8jM8BID
#beginners#java#programming#tutorial#static
Reading 0:00
15s threshold

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.