1:
public class ThrowsDemo {
static void throwMethod(){
System.out.println("Inside throwMethod");
throw new IllegalAccessException("demo");
}
public static void main(String [] args){
try{
throwMethod();
}
catch(IllegalAccessException e){
System.out.println(Caught"+e);
}
}
}
answer: Compile Error Why?????????2:
public static void main(String [] args){
char digit='a';
for(int i=0;i<10;i++){
switch(digit){
case 'x':{
int j=0;
System.out.println(j);
}
default:{
int j=0;
System.out.println(j);
}
}
}
int i = j;
System.out.println(i);
} 输出什么???
3:
public class MyClass{
private static int x = getValue();
private static int y = 5;
private static int getValue(){
return y;
}
public static void main(String [] args){
System.out.println(x);
}
}answer: prints 0 为什么x输出0?????????
public class ThrowsDemo {
static void throwMethod(){
System.out.println("Inside throwMethod");
throw new IllegalAccessException("demo");
}
public static void main(String [] args){
try{
throwMethod();
}
catch(IllegalAccessException e){
System.out.println(Caught"+e);
}
}
}
answer: Compile Error Why?????????2:
public static void main(String [] args){
char digit='a';
for(int i=0;i<10;i++){
switch(digit){
case 'x':{
int j=0;
System.out.println(j);
}
default:{
int j=0;
System.out.println(j);
}
}
}
int i = j;
System.out.println(i);
} 输出什么???
3:
public class MyClass{
private static int x = getValue();
private static int y = 5;
private static int getValue(){
return y;
}
public static void main(String [] args){
System.out.println(x);
}
}answer: prints 0 为什么x输出0?????????
解决方案 »
- 一款iOS平台下的求路径游戏,想用java求解,不知道是否可行,各路大神帮忙看看
- Hibernate C3P0连接池问题?
- Spring注释驱动模式下如何进行代码混淆
- JComboBox无法动态改变
- 高分相送
- 超级问题
- 调制解调器编程问题
- 就是我有个mail收发程序,要求在windows,linux,unix上都能作为系统服务使用。就是说在用户登陆之前就在运行了,怎么做啊
- 新手求叫实现一个action事件中的算法
- 请教:在SQL server 的 JDBC 中怎样设置才能使用ResultSet.isBeforeFirst
- JSP搭建本地环境报错问题,大家进来看看
- java 编写的判别素数的代码,但是不知道开根的函数
第2个:作用域的问题!
第3个:初始化x时还没有y呢,把x=get..()放后面试试!
{
static void throwMethod() throws IllegalAccessException
{
System.out.println("Inside throwMethod");
throw new IllegalAccessException("demo");
}
public static void main(String [] args)
{
try
{
throwMethod();
}
catch(IllegalAccessException e)
{
System.out.println("Caught"+e);
}
}
}
第2个:作用域的问题,j会提示未定义;
第二个题是由于java是按行一行一行的编译的所以x就没有初始化呀故会输出0;
完全统一楼上的观点
private static int x = getValue();
private static int y = 5;
private static int getValue(){
return y;
}
static {
System.out.println("first: "+y);
} public static void main(String [] args){
System.out.println(x);
}