【读书笔记】Core Java for the Impatient 第二章

Object-Oriented Programming

  • Static variables don’t belong to any objects. Static methods are not
    invoked on objects.

  • An inner class is a nonstatic nested class. Its instances have a reference to the object of the enclosing class that constructed it.

  • You can never write a method that updates primitive type parameters.


Default Initialization

Instance variable is automatically set to a default value: numbers to 0,boolean values to false,and object references to null.
In this regard,instance variables are very different from local variables. Recall that
you must always explicitly initialize local variable

Static Variables and Methods

Since static methods don’t operate on objects,you cannot access instance variables from a static method. However,static methods can access the static variables in their class

Packages

It is a good idea to run javac with the -d option. Then the class files are generated in a separate directory,without cluttering up the source tree,and they have the correct subdirectory structure.

A source file can contain multiple classes,but at most one of them can be declared public. If a source file has a public class,its name must match the class name.

Nested and Inner Classes

Nested Classes

Use a static nested class when the instances of the nested class don’t
need to know to which instance of the enclosing class they belong. Use an inner class only
if this information is important.


public class Invoice {
    private static class Item { // Item is nested inside Invoice
        String description;
        int quantity;
        double unitPrice;
        double price() { return quantity * unitPrice; }
    }
    private ArrayList items = new ArrayList<>();
    …
}

There is nothing special about the Item class,except for access control.
If you change the keyword private into public,there is essentially no difference between this Invoice.Item class and a class InvoiceItem declared outside any other class.
Nesting the class just makes it obvIoUs that the Item class represents items in an invoice.


Inner Classes

public class Network {
    public class Member {
        …
        public void leave() {
            members.remove(this);
        }
    }
    private ArrayList members;
    …
}

A method of an inner class can access instance variables and methods of its outer class. In this case,they are the instance variables of the outer class object that created it

Comments

//TODO

相关文章

这个问题和curl无法访问https资源是类似的,现在curl可以访问https资源,但是使用pecl安装扩展的时候不行...
在浏览器输入chrome://flags/回车,找到Omnibox UI Hide Steady-State URL Scheme and Trivial Subdoma...
方法一: 我们都知道Ubuntu有一个专门用来安装软件的工具apt,我们可以用它来全自动安装arm-linux-gcc。...
中文的windows下的cmd默认使用GBK的编码,敲代码时,页面使用的是UTF-8(65001),而powershell控制台默认...
提示错误: arm-linux-gcc:Command not found PATH里有/usr/oca/arm/bin,但是make的时候,就是找不到 a...
我在Graph API开发中用的最多的测试工具就是Graph Explore,这个是微软开发的网页版的Graph API的测试工...