在方法中使用静态块。 [英] Use of static block in methods.
本文介绍了在方法中使用静态块。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基本上正在完善,完成并尝试从java初学者的参考书中编译测试代码。目标是学习静态块的工作。
I'm basically refining, completing and trying to compile a test code from a reference book for java beginners. The objective is to learn the job of static block.
class TimePass3
{
void show1()
{
int a;
static
{
System.out.println("Hi1");
}
}
void show2()
{
static
{
System.out.println("Hi2");
}
}
public static void main(String[] args)
{
show1();
show2();
static
{
System.out.println("Hi2");
}
}
}
编译此代码时我收到错误
While compiling this code I am getting a error at
"Illegal start of expression static"
此代码中的错误是什么?
我尝试了什么:
请帮我解决这个问题。
谢谢。
What is the mistake in this code?
What I have tried:
Please help me resolve this.
Thanks.
推荐答案
请参阅初始化字段(Java™教程>学习Java语言>类和对象) [ ^ ]。
据我所知,这样的静态块(内部方法)在 Java
编程语言。相反,您可能有一个静态块作为类初始化器,请参阅,例如: Java - 静态类,块,方法和变量 [ ^ ]。
As far as I know, such static blocks (inside methods) are illegal in Java
programming language. Instead, you may have a static blocks as class initializers, see, for instance: Java - Static Class, Block, Methods and Variables[^].
这篇关于在方法中使用静态块。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文