• 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

What does this code do?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get what you trying to say
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code
--------------
class Alpha {
System.out.println("hi");
}
----------------

this will not compile
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, did you read the tutorial page that I linked to? This follow-up question is explained too.

Henry
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic