• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

help

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me why the following code can not be
compiled?
public class Base {
System.out.println( "We hit infinity!");
}
Compile error:Type expected.
Would you explain this for me? Thanks in advance!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that's 'coz you need to have methods enclosing all the statements. You do not have a main method in your code. statements can be placed in either constructors, initializers, methods etc.
Hope this helps!
~Kavitha
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried compiling this -
public class Base
{
{
System.out.println( "We hit infinity!");
}
}
I am quoting from the JLS -
Class Body and Member Declarations
A class body may contain declarations of members of the class, that is, fields (�8.3) and methods (�8.4). A class body may also
contain static initializers (�8.5) and declarations of constructors (�8.6) for the class.
ClassBody:
{ ClassBodyDeclarationsopt }
ClassBodyDeclarations:
ClassBodyDeclaration
ClassBodyDeclarations ClassBodyDeclaration
ClassBodyDeclaration:
ClassMemberDeclaration
StaticInitializer
ConstructorDeclaration
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
The scope of the name of a member declared in or inherited by a class type is the entire body of the class type declaration.
You can have a look at http://java.sun.com/docs/books/jls/html/8.doc.html#15372

[This message has been edited by Ankur (edited July 03, 2000).]
 
Leon Peeters
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appreciate for your comments, Kavitha and Ankur!
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys.....
i could compile leon's code correctly..... no compiler error...
???
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me also it is giving compilation error
" Type expected".
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try this :
public class Base{
static{
System.out.println("Hello");
}
}
It compiles, prints Hello , and says that main(String[]) not defined.
;-)
 
There's a city wid manhunt for this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic