• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access modifier in main method not allowed. Why?

 
Kurt Hahn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this is an exercise I wrote:

public class Konverter {
//private Euro e = new Euro(14.4);
public static void main (String[] args){
private Euro e = new Euro(14.4);
}
}

Compiler:
Konverter.java:12: illegal start of expression
public Euro e = new Euro(14.4);
^
I I remove 'public', there's no error. Also, if I remove the slashes in the second line, it gets compiled. I think I missed something important here: Why is it there are no modifiers allowed here?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accesss modifiers are to be used in front of instance level variables and instance level methods.

So, anything that is declared inside of a class, but NOT insider of a method, is essentially 'instance level,' or 'of the class.' Things that are 'of the class' can have access modifiers.

Things inside methods are 'of the method.' So, variables inside of methods cannot have access modifiers applied to them.

This is perhaps a bit of an oversimplification, but it holds pretty true for the most part, and it certainly explains why you are getting the exception.

-Cameron McKenzie
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use access specifiers such as public and private for local variables. Correct me if I am wrong.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's a very true statement.

-Cameron McKenzie
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic