据我所知,"静态初始化块"用于设置静态字段的值,如果不能在一行中完成的话.
但我不明白为什么我们需要一个特殊的块.例如,我们将一个字段声明为静态(没有值赋值).然后编写几行代码,生成并为上面声明的静态字段赋值.
为什么我们需要在一个特殊的块这样的行这样的:static {...}?
java static initialization static-block initialization-block
默认构造函数与直接初始化对象字段之间的区别是什么?
有什么理由更喜欢以下示例中的一个而不是另一个?
public class Foo
{
private int x = 5;
private String[] y = new String[10];
}
Run Code Online (Sandbox Code Playgroud)
public class Foo
{
private int x;
private String[] y;
public Foo()
{
x = 5;
y = new String[10];
}
}
Run Code Online (Sandbox Code Playgroud) Java中是否存在匿名代码块的实际用途?
public static void main(String[] args) {
// in
{
// out
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这不是关于命名块,即
name: {
if ( /* something */ )
break name;
}
Run Code Online (Sandbox Code Playgroud)
.
我正在学习java并且偶然地遇到了以下代码,其中在方法之后执行默认构造函数.
public class ChkCons {
int var = getVal();
ChkCons() {
System.out.println("I'm Default Constructor.");
}
public int getVal() {
System.out.println("I'm in Method.");
return 10;
}
public static void main(String[] args) {
ChkCons c = new ChkCons();
}
}
输出:
I'm in Method. I'm Default Constructor.
有人可以解释一下为什么会这样吗?
谢谢.
根据SCJP6(页577),我发现实例变量在超类构造函数完成之前被赋予了默认值,我在Debugg模式中尝试了一个例子,但我看到超级承包商在实例变量获取其默认值之前运行,可以任何一个解释那对我来说?
我想用的例子:
package courseExercise;
class test {
test() {
System.out.println("Super Constructor run");
}
}
public class Init extends test {
private Integer i = 6;
private int j = 8;
Init(int x) {
super();
System.out.println("1-arg const");
}
Init() {
System.out.println("no-arg const");
}
static {
System.out.println("1st static init");
}
public static int d = 10;
{
System.out.println("1st instance init");
}
{
System.out.println("2nd instance init");
}
static {
System.out.println("2nd static init");
}
public static void main(String[] args) {
new Init();
new Init(7); …Run Code Online (Sandbox Code Playgroud) 这是代码:
public class Main {
public static void main(String[] args) {
new B();
}
}
class A {
A() {
System.out.println("A constructor before");
action();
System.out.println("A constructor after");
}
protected void action() {
System.out.println("Never called");
}
}
class B extends A {
private final int finalField = 42;
private int field = 99;
B() {
System.out.println("B constructor");
action();
}
public void action() {
System.out.println("B action, finalField=" + finalField + ", field=" + field);
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
A constructor before
B action, …Run Code Online (Sandbox Code Playgroud) 我正在学习有关如何使用this()重载的构造函数,并遇到了以下限制:
您不能在调用this()时使用构造函数类的任何实例变量。
例如:
class Test{
int x;
public Test() {
this(x); //Does not compile
}
public Test(int y) {}
void method1() {
method2(x); //OK
}
void method2(int y) {}
}
Run Code Online (Sandbox Code Playgroud)
我知道不需要将实例字段传递给构造函数,因为默认情况下它是可见的。但是,为什么相同的限制不适用于实例方法?
如果我编写以下代码,为什么输出为0.0?在我看来,它不会读取我的 Car 类中的变量 x 。
\npublic class Car {\n\ndouble x = 2; \ndouble y;\ndouble v = x * y; \n\npublic Car(double y) {\nthis.y = y; \n}\n\npublic static void main(String[]\xc2\xa0args) {\nCar car = new Car(100);\nSystem.out.println(car.v);\n}\n\n}//end of class\nRun Code Online (Sandbox Code Playgroud)\n 我的回归价值不是正确的,我只是想知道我做错了什么,我的代码看起来很好?!
一些即将到来的测试是:
Test failed: expected was not equal to actual
The discriminant is not being computed correctly
Test failed: expected was not equal to actual
public class QuadraticEquation {
//coefficients
private double a;
private double b;
private double c;
// created a discriminant instance variable so it's easier to access
// rather than tying out "Math.pow(b, 2) - 4 * a * c" every time
// the discriminant is required
private double discriminant = …Run Code Online (Sandbox Code Playgroud)