代码查错

class Something {
    int i;
    public void doSomething() {
        System.out.println("i = "+ i);
    }
} 
答案: 正确。输出的是"i = 0"。int i 属于 instant variable (实例变量,或叫成员变量)。instant

variable 有 default value。int 的 default value 是0。

class Something {
final int i;
    public void doSomething() {
        System.out.println("i = "+ i);
    }
} 
答案: 错。final int i 是个 final 的 instant variable (实例变量,或叫成员变量)。final 的 instant
variable 没有 default value,必须在 constructor (构造器)结束之前被赋予一个明确的值。可
以修改为"final int i =0;"。
public class Something {
    public static void main(String[] args) {
        Something s = new Something();
        System.out.println("s.doSomething() returns " + doSomething());
    }
    public String doSomething() {
        return "Do something ...";
    }
} 
答案: 错。看上去在 main 里 call doSomething 没有什么问题,毕竟两个 methods 都在同一个 class 里。但仔细看,main 是 static 的。static method 不能直接 call non-staticmethods。可改成"System.out.println("s.doSomething()returns " + s.doSomething());"。同理,staticmethod 不能访问 non-static instant variable。

Something1.java

class Something {
    private static void main(String[] something_to_do){
        System.out.println("Dosomething ...");
    }
} 
答案: 正确。从来没有人说过 Java 的 Class 名字必须和其文件名相同。但 public class 的名字必须和文件名相同。
(单选题)Java程序中的类名称必须与存放该类的文件名相同。
A   对
B   错

答案: B 
声明为public类型的类名必须与文件名相同,默认权限的可以不同
并且内部类的类名一般与文件名不同

5。

interface A{
    int x = 0;
}
class B{
    int x =1;
}
class C extends B implements A {
    public void pX(){
        System.out.println(x);
    }
    public static void main(String[] args) {
        new C().pX();
    }
} 
答案:错误。在编译时会发生错误(错误描述不同的 JVM 有不同的信息,意思就是未明确的x 调用,两个 x 都匹配(就象在同时 import java.util 和 java.sql 两个包时直接声明 Date 一样)。对于父类的变量,可以用 super.x 来明确,而接口的属性默认隐含为 public staticfinal.所以可以通过 A.x 来明确。
interface Playable {
    void play();
}
interface Bounceable {
    void play();
}
interface Rollable extends Playable, Bounceable {
    Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
    private String name;
    public String getName() {
        return name;
    }
    public Ball(String name) {
 
        this.name =name;
    }
    public void play() {
        ball = newBall("Football");
        System.out.println(ball.getName());
    }
} 
答案:错。"interfaceRollable extends Playable, Bounceable"没有问题。interface 可继承多个 interfaces,所以这里没错。问题出在 interface Rollable 里的"Ball ball =newBall("PingPang");"。任何在 interface 里声明的 interface variable (接口变量,也可称成员变量),默认为 public static final。也就是说"Ball ball = new Ball("PingPang");"实际上是"publicstaticfinal Ball ball = new Ball("PingPang");"。在 Ball 类的 Play()方法中,"ball =newBall("Football");"改变了ball的reference,而这里的ball来自Rollable interface,Rollableinterface 里的 ball 是 public static final 的,final 的 object 是不能被改变 reference 的。因此编译器将在"ball = newBall("Football");"这里显示有错。

static关键字

static修饰成员变量

  • 用static修饰的成员变量不属于对象的数据结构;
  • static变量是属于类的变量,通常可以通过类名来引用static成员;
  • static成员变量和类的信息一起存储在方法区,而不是在堆中,一个类的static成员变量只有“一份”,无论该类创建了多少对象。
class Cat{
  private int age;
  private static int numOfCats;
  public Cat(int age){
    this.age = age;
    System.out.println(++numOfCats);
  }
}
JAVAJSD_V04OOPDAY05_018.png

static 修饰方法

  • 通常的方法都会涉及到具体对象的操作,这些方法在调用时,需要隐士的传递对象的引用(this)。
2018-01-01 14-23-22屏幕截图.png
  • static修饰的方法则不需要针对某些对象进行操作,其运行结果仅仅与输入的参数有关,调用时直接用类名引用。

    double c = Math.sqrt(3.0*3.0+4.0*4.0);

该方法在调用时,没有隐式的传递对象引用,因此在static方法中不可以使用this关键字。

  • 由于static在调用时没有具体的对象,因此在static方法中不能对非static成员(对象成员)进行访问。

    ... ... ...
    Point.distance(Point p1,Point p2)
    RandomUtils.nextInt()
    StringUtils.leftPad(String str,int size,char padChar)
    ... ... ...
    Math.sqrt() Math.sin() Arrays.sort()
    

static块

  • static块: 属于类的代码块,在类加载期间执行的代码块,只执行一次,可以用来在软件中加载静态资源。

    class Foo {
      static {
          //类加载期间,只执行一次
          System.out.println("Load Foo.class");
      }
      public Foo() {
          System.out.println("Foo()");
      }
    }
    

    输出的结果为:

    Load Foo.class

    Foo();

    Foo类加载时,运行静态块,在创建对象之前。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,217评论 18 399
  • (一)Java部分 1、列举出JAVA中6个比较常用的包【天威诚信面试题】 【参考答案】 java.lang;ja...
    独云阅读 11,943评论 0 62
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • 狼族的正厅里,一个身着黑色长袍,脚穿黑色马丁靴的男人正坐在王椅上,转动手里的血钻,像是在思考着什么,这个男人有一张...
    沫鹿阅读 3,233评论 0 0
  • 【原文】 (18.7) 子路从而后,遇丈人,以杖荷蓧。子路问曰:“子见夫子乎?”丈人曰:“四体不勤,五谷不分...
    钱江潮369阅读 3,051评论 2 1