Results 1 to 2 of 2
  1. #1
    truely is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Confused about declaring static int

    Hello,

    I am really just a beginner with programming and I am currently following a beginner's tutorial.
    I accidentally wrote a code differently than what I should've, and I got these errors :
    "Illegal modifier for parameter a; only final is permitted"
    "Illegal modifier for parameter b; only final is permitted"
    "Illegal modifier for parameter c; only final is permitted"

    The code that I wrote and gave these errors:
    Java Code:
    class Math {
    
    	public static void main(String[] args) {
         
    	static int a = 11;
    	static int b = 35;
    	static int c = 29;
       //the rest of the code below
    }
    }

    And the code that was supposed to be written (and worked after I fixed it):
    Java Code:
    class Math {
    
    	static int a = 11;
    	static int b = 35;
    	static int c = 29;
    	
         public static void main(String[] args) {
    
       //the rest of the code below
    }
    }
    I noticed that I can declare "static int" only under "class Math" and not under "public static void main".(I had to remove "static" if declaring int under "public static void main");
    Could someone please explain to me why is that?

    Thanks

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

    Default Re: Confused about declaring static int

    As a modifier, static applies to class members (fields and methods). It means the member is conceptually part of the class, not part of instances of the class. Variables declared inside methods are not class members, so they can't be static.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 6
    Last Post: 09-08-2012, 01:08 AM
  2. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  3. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •