• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access modifier in main method not allowed. Why?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's a very true statement.

-Cameron McKenzie
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic