• 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

static void main(String args[]) is working why??????

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why this code is working without the proper signature of main() method??
I am confused???
public class Tester
{
int i;
static final int c;
static int x;
Tester(){}//Ist constructor
Tester(char c) //IInd constructor
{
}
static//Ist static initializer block
{
System.out.println("Static init 1");
}
static //IInd static initializer block
{
System.out.println("Static init 2");
c =1;
}
static //IIIrd static initializer block
{
System.out.println("Static init 3, c ="+c);
}
{
System.out.println("Instatnce init 2 ");
}
{
System.out.println("Instatnce init 1 ");
}
static void main(String args[])
{
System.out.println(x);
new Tester();
System.out.println(new Tester().i);
}
}
The output is coming
Static init 1
Static init 2
Static init 3, c =1
0
Instatnce init 2
Instatnce init 1
Instatnce init 2
Instatnce init 1
0
Thanks in advance
Amit
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit.

Its very code for static initilization.Coming to your question
"Why this code is working without the proper signature of main() method??"
Actually JVM never checks for the access modifiers of the main method.I don't know still why its doing.Somebody says its a loop hole in the JVM.
You can use any of the access modifiers for the main method, like,
1)public
2)private
3)protected.
all give you the same result.Is't it misterious.
If anybody knows the correct anwers pls advice me.
Regards
Prasad
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even the JVM spec states that the method main must be declared public, static, and void. Not sure, how this condition is relaxed in these cases. May be someone who knows JVM very well can help understand this behavior.
Anbarasu
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit,
Regarding this issue Ajith has posted a mail in FAQ's. It seems they logged this issue with SUN. Correct me if I am wrong.
Thanks,
~Srini
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think it is a convention that should be followed.
however in the exam stick to
public static void main(String args[]) or one of its varieties.
 
Amit Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you all guys..
I really got shocked when,I notice this.
I also think that from exam point of view, we should consider only public static void main(String args[]) right.
But, Surprisingly Why SUN is insisting on maintaining the right signature?

Amit
 
Without deviation from the norm, progress is not possible - Zappa. 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