Andrew Vershinin

Greenhorn
+ Follow
since Feb 16, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Andrew Vershinin

Viktor Kubinec wrote:

Andrew Vershinin wrote:

Campbell Ritchie wrote:Welcome to the Ranch


2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.



This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)


I tried:
Test.java


javac Test.java
java -cp . Test

Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)


12 years ago

Campbell Ritchie wrote:And what if you try it in Java6?


The same thing.
12 years ago

Campbell Ritchie wrote:Welcome to the Ranch


Thanks

Campbell Ritchie wrote:What happens if you try that in Java7? Particularly if you miss out the main method?


1. AFAIK, Java 7 supports static initializers, so it will work as usual - after loading of the class.
2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.
Excuse me for my broken English
12 years ago
Use static initializer.

static {
//your code goes here...
}
12 years ago