PS:
This question has been edited a few times as my previous code doesn't demonstrate the problem. There are some answers which may not make perfect sense against the edited question
I have a public class named Son.java
Son.java
package com.t; public class Son extends Father { static int i; static { System.out.println("son - static"); i = 19; } { System.out.println("son - init-block"); } public static void main(String[] args) { //Son s = new Son(); int a[] = new int[2]; System.out.println(a[5]); } } class Father { static { System.out.println("f - static"); } { System.out.println("f - init-block"); } }
When I run the program for the first time:
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at com.t.Son.main(Son.java:19) f - static son - static
And later, when I run this program (output order random)
random
f - static son - static Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at com.t.Son.main(Son.java:19)
I read that blocks staticare executed as classes are initialized.
static
But why is the first exception raised here and then a static block executed?
I use Eclipseto run my program. Can someone explain?
Eclipse
The exception does not occur at first, you just see the listing of the exception in the first place.
If the exception occurred first, you would never see the rest of the output.
, System.err ( ), System.out . , , , .
System.err
System.out
System.err, . System.out, , , .
System.err, , .
@ Keppil .
-... erm... .
:
Eclipse .
, " "... , → < < . , stdout/stderr , "Eclipse".
, stderr stdout, , - . , , , syscall select ... , . , , .
select
Eclipse, Eclipse, , . , read syscalls, , , , . , select... .
read
, , stdout/stderr Eclipse, "" .
As asser said, mainbelongs to the class Sonand spreads Father. I changed the code a bit, so I was able to compile.
main
Son
Father
class Father { static{ System.out.println("f - static"); } } public class Son extends Father { static { System.out.println("son - static"); } public static void main(String[] args) throws ArrayIndexOutOfBoundsException{ int a[] = new int[2]; System.out.println(a[3]); } }
And the result: -
f - static son - static Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at kanwal.Son.main(Son.java:20)
It works exactly as intended.
EDIT: - This answer was made before the OP edited the question.
Source: https://habr.com/ru/post/1533112/More articles:sort mysql table column value by part of its value - sqlIs EOF errno installed? - cHow to open the Rad menu after clicking on it? - .netHow to share images with my application in whatsapp? - androidUserControl enum DependencyProperty не является обязательным - enumsAndroid: ACTION_SEND_MULTIPLE with com.android.email - androidCannot start mvn clean - javaIs there any implementation of CalDAV + CardDAV server in .NET? - c #Trying to make a simulation in R - rSet the dynamic Apple menu name for Java program in NetBeans 7.4 - javaAll Articles