GeeCON Prague 2014*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes access modifier in main method not allowed. Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "access modifier in main method not allowed. Why?" Watch "access modifier in main method not allowed. Why?" New topic
Author

access modifier in main method not allowed. Why?

Kurt Hahn
Greenhorn

Joined: May 28, 2008
Posts: 3
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Jan 02, 2008
Posts: 190
You cannot use access specifiers such as public and private for local variables. Correct me if I am wrong.


Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I think that's a very true statement.

-Cameron McKenzie
 
GeeCON Prague 2014
 
subject: access modifier in main method not allowed. Why?