Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Simple code  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Simple
{
//4
System.out.println("Here"); //Does not work.!!!
{
System.out.println("Here"); //Works. Why ??
}

public void test()
{
System.out.println("HERE");
}
public static void main(String args[])
{
Simple obj = new Simple() ;
}
}
**********
Above given code will not compile,
but it will work just fine if I enclose the line
//4 within initializer block.
Please enlighten.
Thanks.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not allowed to have normal expressions simply "lying about" in your class. According to the JLS, §8.1.5 Class Body and Member Declarations:


A class body may contain declarations of members of the class, that is, fields (�8.3), classes (�8.5), interfaces (�8.5) and methods (�8.4). A class body may also contain instance initializers (�8.6), static initializers (�8.7), and declarations of constructors (�8.8) for the class.


Therefore, if you want to put that expression in your class, it must go in either a method, an instance initializer, a static initializer, or a constructor.
I hope that helps,
Corey
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words the second one works because you have effectively made it into a static block, however the first one will not compile as it is neither static nor enclosed within a method.
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words the second one works because you have effectively made it into a static block
not a static block but an instance initializer.
A static block is declared as follows
static {
...
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statement in question is not an assignment statement or a method signature therefore it cannot exist outside a static or instance block. Such a statement will display "HERE" on a screen, if executed properly, thus it needs to be enclosed within a block (a block is any code or statements found within { } braces).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!