Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does this code do?

 
abin joy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code
-------------
public class Top {
{
System.out.println("hi");
}

}
----------------------
code

Why compiler is not showing any error in this code?Why dont this piece of code require a function to compile properly?
 
Henry Wong
author
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is in an "instance initializer" which is executed during the construction process -- after the super() of the contructor has completed, but before the rest of the constructor has executed.

Henry
 
abin joy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt get what you trying to say
 
Henry Wong
author
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the link to the relevant chapter in the Sun Java tutorial.

http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html

Maybe you can start there...

Henry
 
abin joy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why
code
--------------
class Alpha {
int over=1;
}
--------------
is allowed where as

code
--------------
class Alpha {
System.out.println("hi");
}
----------------

is not allowed
 
ravikdcs dixit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code compiles fine
but not results in any output
since there is no public static void main() method
the statement in the main() method are only executed by the interpreter or JVM
 
abin joy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code
--------------
class Alpha {
System.out.println("hi");
}
----------------

this will not compile
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by abin joy:
but why
code
--------------
class Alpha {
int over=1;
}
--------------
is allowed where as

code
--------------
class Alpha {
System.out.println("hi");
}
----------------is not allowed
int over = 1; is a declaration and initialisation, not an ordinary statement.
System.out.println("hi"); is an ordinary statement. No ordinary statements allowed outside methods and initialisers.
 
Henry Wong
author
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, did you read the tutorial page that I linked to? This follow-up question is explained too.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic